I have a question getting checked boxes in asp.net mvc controller...
I have a form that contains many checkboxes with values.. So the user can check as many boxes he wants, and what I need is those values from the checked boxes... What I tried is getting
public ActionResult actionName(FormCollection form){...}
I do not even know how to access the checkbox value that is being passed into the function...
I have been looking for answer but could not find one.. How do I get it in the controller... I am using C# by the way.
-- edit
I have list of data and pass it into the view... Then, I create the checkbox as
<input type="checkbox" name="CheckedCart" value="#ValueForBox" />
where #ValueForBox is the value that I need to obtain in the controller if it is checked..
** I did not realize it was an old post while answering. But, it may help.
I assume you have have multiple checkboxes in the View with same name.
and you need values of the selected checkboxes in controller on form submit.
if yes, then below will help you.
in controller
[HttpPost]
public ActionResult Index(List<string> CheckedCart)
{
}
and in view
#using (Html.BeginForm())
{
<input type="checkbox" name="CheckedCart" value="value1" />
<input type="checkbox" name="CheckedCart" value="value2" />
<input type="checkbox" name="CheckedCart" value="whatever the value" />
<input type="submit" id="btnSave" value="Save" />
}
When form is submitted. You will get the values of selected checkboxes in parameter List<string> CheckedCart in index method. and values for checkboxes which are not selected will be false.
so, as per above example
if second checkbox is checked and other are false.
in the List<string> CheckedCart you will get
[0] false
[1] value2
[2] false
Related
I tried to passing a request from a view to another view. It's work, as I check from dd(). But the problem is, there's an error when I try to get the file from the request of the second view.
Here is the error
Here's how I send the request from the first view to the second view by controller:
return view('preview-data', ['request' => $request]);
Here's how I set the form value in the second view :
<form action="{{ route('export') }}" method="POST" enctype="multipart/form-data">
#csrf
<input type="hidden" class="form-control-file" name="excelFile" id="excelFile"
value="{{$request->excelFile}}">
<button type="submit" class="btn btn-primary">
<i class="bi bi-file-earmark-pdf-fill"></i>
Export PDF
</button>
</form>
this is what it looks like if I try to use #dd($request->excelFile) inside the second view
If I try to dd($request->excelFile) in the second view controller, it will only return the pathname value.
Also it seems like the only value that will not change even if I refresh the page is just the originalName attribute. The pathname attribute and the others is always changing its value.
Is there any way that I could get the value of the file input in the second view exactly the same as the first view?
I'm trying to dynamically generate a series of checkboxes in hbs. I have a bunch of arrays stored on a database and if the arrays contain the right numbers, I want a checkbox to be generated that has already been checked.
I've tried making a helper that just returns array.includes(num); and I've verified that everything works on that through logs, but if I plug my helper into an #if tag, I get one of many designer errors ranging from array.includes is not a function to ((depth0 && depth0.days_of_week) || alias2).call is not a function. I've even tried making a bunch of semi-static helpers so that I only need to pass one parameter instead of two and that didn't make any difference.
hbs.registerHelper('daysOfWeek', (array, dayNum) => {
return array.includes(dayNum);
});
...
<input type="checkbox" name="daysOfWeek" value="0" {{#if daysOfWeek (this.days_of_week 0)}}checked{{/if}}>Sunday
<input type="checkbox" name="daysOfWeek" value="1" {{#if daysOfWeek (this.days_of_week 1)}}checked{{/if}}>Monday
...
The syntax should be {{#if (daysOfWeek days_of_week 0)}}checked{{/if}}. Your helper function is correct.
<input type="checkbox" name="daysOfWeek" value="0" {{#if (daysOfWeek days_of_week 0)}}checked{{/if}}>Sunday
<input type="checkbox" name="daysOfWeek" value="1" {{#if (daysOfWeek days_of_week 1)}}checked{{/if}}>Monday
The if statement is checking the whole return of something, so in your case it is the helper function with two parameters: (daysOfWeek days_of_week 0).
How to call the controller from the radio button click ?
<div id="radio">
<input type="radio" id="Isactive" name="Isactive" value="1" />Yes</label>
<input type="radio" id="Isactive" name="Isactive" value="0" />No</label>
</div>
public ActionResult Index(CityList Obj,string Isactive)
{
return view();
}
You can use javascript to programatically submit a form. You should not have duplicate Id values for elements. Id's should be unique in a document.
#using (Html.BeginForm("Index", "Home"))
{
<!-- Your other form elements-->
<div id="radio">
<input type="radio" name="Isactive" value="Yes" />Yes
<input type="radio" name="Isactive" value="No" />No
</div>
}
Now you can listen to the change event on the radio button and use jQuery closest method to get the form and call the submit method.
$(document).ready(function () {
$("input[name='Isactive']").change(function () {
$(this).closest("form").submit();
});
});
Now when user make a selection on the radio button, the form will be submitted and the selected radio button's value attribute value will be available in your Isactive parameter of your HttpPost index action method.
If you want to pass 1 and 0, use a numeric type as the method parameter type instead of string.
I have a checkbox in my view that I am posting back to the Controller as a parameter, but when the checkbox is checked the controller receives the value as a string "on", and when unchecked the value in the parameter is "off". Is there any way to have this value postback as an int or bool? When I try bool below it is always null because it doesn't match the data type.
<label><input type="checkbox" name="option">food</label>
public ActionResult Select(bool? option)
{....
I guess you need to use the Html checkbox helper for this to work, which seems silly to me because it renders and works correctly client side when you create html without using the helper.
<label>#Html.CheckBox("option", false) food</label>
What you can do is add value attribute to your checkbox and set it to true or false.
MVT will not recognize option as true here
<input type="checkbox" name="option" checked="checked" />
but will if you add value="true"
<input type="checkbox" name="option" checked="checked" value="true" />
Script that does it:
$(document).on("click", "[type='checkbox']", function(e) {
if (this.checked) {
$(this).attr("value", "true");
} else {
$(this).attr("value","false");}
});
Hi I want to show and hide div using alloy ui onclick function of radio button.I mentioned my code as below.please help me to solve this problem.when i clicked on married i want to open one div it has anniversary date and when I click on single it will hide that div.
<aui:field-wrapper name="maritial_status">
<aui:input checked="<%= true %>" inlineLabel="right" name="maritial_status" type="radio" value="single" label="single" onClick="javascript:unMarriedStatus();" />
<aui:input inlineLabel="right" name="maritial_status" type="radio" value="married" label="married" onClick="javascript:marriedStatus();" />
</aui:field-wrapper>
You can solve this issue by having the function and the div class
so based on the selection call the function which will implicitly create the structure form in div method
function design:
function temp()
{
var chasisno = "";
$('#dyanamicDivElement').append(chasisno);
}
div structure:
Explanation:
so as per here the function will keep appending the value to the div
hope this solved the issue
You could try this way:
<aui:field-wrapper name="maritial_status">
<aui:input checked="<%= true %>" inlineLabel="right" name="maritial_status" type="radio" value="single" label="single" onClick="document.getElementById('DIV-ID').style.display = this.checked ? 'none' : 'block';" />
<aui:input inlineLabel="right" name="maritial_status" type="radio" value="married" label="married" onClick="document.getElementById('DIV-ID').style.display = this.checked ? 'block' : 'none';" />
</aui:field-wrapper>