I've got a form similar to the below, the name attributes have been set with MVC model binding in mind.
At the moment I'm only able to select one radio between the two forms. I want to select a radio button for each form, i.e. one radio button selected per form at any given time. I can't find a way to do this whilst keeping my name attribute naming convention.
Any ideas, thanks.
<div class="form-group-1">
<input type="radio" name="Students[0].Answer1" value=""><br>
<input type="radio" name="Students[0].Answer2" value=""><br>
</div>
<div class="form-group-2">
<input type="radio" name="Students[1].Answer1" value=""><br>
<input type="radio" name="Students[1].Answer2" value=""><br>
</div>
Related
I am wanting to redirect to another page but at the same time being able to grab the details of the button that was selected. I was reading up on how onsubmit works with HTML and radio buttons work. Prior to adding buttons, I had a button and whenever it was clicked it would redirect me to the next page. I still want to do the same thing, just being able to add radio buttons to the view and submit that radio button so that way I can grab the information from the button that was selected.
I attempted:
#{
ViewData["Title"] = "Index";
}
<h2>Customer</h2>
<form method="POST">
<input type="radio" value="1" /><label>Valid</label>
<input type="radio" value="2" /><label>Wrong</label>
<input type="radio" value="3" /><label>InValid</label>
<a href="#("window.location.href='" + #Url.Action("SecIndex", "Second") + "'");">
<input type="submit" value="Address Validation" />
</a>
However, this does not redirect me to the page that I needed it to redirect to. I also noticed that once I select buttons I cannot unselect, is that apart of the radio button feature?
I also noticed that once I select buttons I cannot unselect, is that apart of the radio button feature
Yes. That's how it works.
I still want to do the same thing, just being able to add radio buttons to the view and submit that radio button so that way I can
grab the information from the button that was selected.
If you want to post the selected value to backend, you could set name for radio buttons. Because model binding system will bind value by name.
View:
<form method="POST" asp-action="SecIndex" asp-controller="Second">
<input type="radio" value="1" name="Status"/><label>Valid</label>
<input type="radio" value="2" name="Status"/><label>Wrong</label>
<input type="radio" value="3" name="Status"/><label>InValid</label>
<input type="submit" value="Address Validation" />
</form>
Controller:
public class SecondController : Controller
{
[HttpPost]
public IActionResult SecIndex(string Status)
// you can get "1" or "2" or "3" which based on your checked radio button
{
return RedirectToAction("Privacy");
}
}
HTML doesn't have store capability. you can't grab data without a programming language. But you can click to redirect to another page.
Use the button tag and use the anchor tag in the button, rather than -
need some help on radio buttons. How can i bind 2 radio buttons that are for one particular question but use 2 different fields in the database. For example there is a question that has a Yes/No answer. The yes answer binds to one field, and the No answer to another, they don't bind to a single filed that has a true, false value.
<div class="radio-s">
<input type="radio" ng-value="true" data-name="use_plan"
data-value="true" ng-model="record.offer_plan" />
<label>Yes</label>
</div>
<div class="radio-s">
<input type="radio" ng-value="false" data-name="use_plan"
data-value="true" ng-model="record.offer_plan" />
<label>No</label>
</div>
Thanks in advance.
Attach the name attribute to both of your radio buttons so that only one can be selected at a time and also bind them to the respective fields from your DB
<div class="radio-s">
<input type="radio" name="planGroup" ng-value="true" data-name="use_plan"
data-value="true" ng-model="record.offer_plan_yes" />
<label>Yes</label>
</div>
<div class="radio-s">
<input type="radio" name="planGroup" ng-value="false" data-name="use_plan"
data-value="true" ng-model="record.offer_plan_no" />
<label>No</label>
</div>
I am passing the input data from the .cshtml page to the action method.
Here is my .cshtml page:
#model passingdata
<form asp-controller="Home" method="post" asp-action="About" >
<button class="btn-danger" type="submit" value="Submit"></button>
<div class="form-group">
<label>
<input asp-for="date" placeholder="Date" class="col-md-8" />
</label>
<label>
Select from these Four Classes
<input type="radio" name="class" asp-for="classselect1" id="classselect1" value="classselect1" class="col-md-4" /> <p> #Model.classname1</p>
<input type="radio" name="class" asp-for="classselect2" id="classselect2" value="classselect2" class="col-md-4" /> <p> #Model.classname2</p>
<input type="radio" name="class" asp-for="classselect3" id="classselect3" value="classselect3" class="col-md-4" /> <p> #Model.classname3</p>
<input type="radio" name="class" asp-for="classselect4" id="classselect4" value="classselect4" class="col-md-4" /> <p> #Model.classname4</p>
</label>
</div>
</form>
And here is my controller code which is invoked when i Click on the button.
[HttpPost]
public IActionResult About(passingdata p)
{
ViewData["Message"] = "Your application description page.";
Teacher.classselect1 = p.classselect1;
Teacher.classselect2 = p.classselect2;
Teacher.classselect3 = p.classselect3;
Teacher.classselect4 = p.classselect4;
Teacher.date = p.date;
return View();
}
The input data like date and bool value from the radiobutton is not passing through the object of the class which contain these variables.
Please help me in this.
If i remember correctly, the Name attribute of each radiobutton is how .net MVC will map the values to your model.
In this case, all of your names are "class", which essentially means you have 1 form field named class with 4 options.
I would recommend using the html helper classes, because they will automatically create the proper html for you. The answer to this post should help: When using .net MVC RadioButtonFor(), how do you group so only one selection can be made?
If you dont want to use the helper just remember that when you submit a form, the data that is posted is based on the name of each form field. .Net does some magic in the background to serialize your Model for you, but essentially you are just submitting data in the format "?prop1=val1&prop2=val2".
Update
I figure maybe I should clarify a little better why what you are doing is not working how you expect.
When you post or put data via a form, it passes the input fields (text box, radio button, checkbox, etc...) as either querystring params or are part of the body. Radio buttons work a little differently than other input type. For a radio button, there are multiple input elements, but only one of them is valid. That is handled by using the name attribute. In your case, all of the names are "class", which means that the only thing being passed to the server is a single "?class={val}" (val is the value of which ever radio button is selected).
If your passingdata model had a property called "class", it would be populated. If your goal is to populated all 4 of the classselect properties with different values, you would need the name of each radio button to be different. But if there was only one radio button with each name, then each property could only have 1 value. You would need multiple RadioButtons with the same name to have multiple values (only one of which is selectable for each property).
Hopefully that clarifies what is wrong and gets you in the right direction.
I am using MVC6 and have a checkbox input field in my form, but when the form is submitted the value for the checkbox always gets passed to the ViewModel as false:
Here is how the property is declared in my ViewModel:
[Display(Name = "Include Sales Tax")]
public bool IncludeSalesTax { get; set; }
Here is how the form looks in my MVC6 razor form:
<div class="form-group">
<div class="checkbox">
<label><input asp-for="IncludeSalesTax" type="checkbox" value="">#Html.DisplayNameFor(m => m.IncludeSalesTax)</label>
</div>
</div>
I figured the above would be the best way to follow Twitter Bootstrap standards and use the ASP.NET MVC6 asp-for tag for model binding.
When I submit the form the value for IncludeSalesTax is always false, even when checked. What am I doing wrong?
input type checkbox sends an "on" if it is set. Otherwise it is not sent.
It is important, that you set the value attribute to true. In this case it sends true or nothing, which is perfect to bind to a boolean.
<input type="checkbox" name="yourPropertyName" value="true" checked />
Pinki's answer above is good if the checkbox should default to checked.
If the checkbox should default to unchecked, a little in-line javascript sets the value to true or false upon clicking:
<input name="yourPropertyName" type="checkbox" value="false" onchange="this.value=this.checked">
After letting Visual Studio generate the form based on my ViewModel here is how it does it:
<div class="checkbox">
<input asp-for="isTaxable" />
<label asp-for="isTaxable"></label>
</div>
Additionally, I was missing the closing of my input tag. So it can also be done like this which is the bootstrap preferred way:
<label><input asp-for="isTaxable" type="checkbox" value=""/>#Html.DisplayNameFor(m => m.isTaxable)</label>
The razor view engine normally creates a checkbox and one hidden input using the same name.
You can simply use the html below to ensure you get your desired result:
<div class="form-group">
<div class="checkbox">
<input type="checkbox" value="true" name="IncludeSalesTax" />Include Sales Tax
<input type="hidden" value="false" name="IncludeSalesTax" />
</div>
</div>
I am posting my form to an MVC Controller, where I want to process some changes the user has made on a grid like html structure.
I have checkboxes rendered as simple HTML in my View for each row :
<input type="checkbox" id="cbxR1" checked="checked" />
<input type="checkbox" id="cbxR2" checked="checked" />
I would like to know how I would retrieve a checkbox value after an AJAX post.
I am trying to use the following to retrieve the
HttpContext.Current.Request.Form["cbxR1"]
and am always getting a null, regardless of whether the checkbox was checked or not.
The cbxR1, cbxR2 should be the name of an input element, not the id.
<form ...>
<input type="checkbox" name="cbxR1" checked="checked" />
<input type="checkbox" name="cbxR2" checked="checked" />
</form>