I have a razor view (Framework 4.5, MVC 5) and an html input type=checkbox with the value equal to a model boolean value but instead of true or false it binds "value".
This is my code:
for (int index = 0; index < Model.Item1.Locations.Length; index++)
{
WSTupleOfInt32StringStringBoolean location = Model.Item1.Locations[index];
<div class="editor-field">
<input id="@string.Format("presentation.ServiceInfoes[{1}].Locations[{0}].Key", index, Model.Item2)" type="checkbox" value="@location.ThirdValue" name="@string.Format("presentation.ServiceInfoes[{1}].Locations[{0}].ThirdValue", index, Model.Item2)" />
</div>
<div class="editor-label">
<label for="@string.Format("presentation.ServiceInfoes[{1}].Locations[{0}].Key", index, Model.Item2)">@location.FirstValue</label>
@if (!string.IsNullOrEmpty(location.SecondValue))
{
<a href="@string.Format("//maps.google.com/?q={0}", location.SecondValue)" target="_blank"><img alt="@location.SecondValue" src="@string.Format("//maps.google.com/maps/api/staticmap?center={0}&markers=color:blue|{0}&zoom=15&size=64x64&sensor=false", location.SecondValue)" /></a>
}
</div><br />
}
The location.ThirdValue
is the boolean property, debuging the property it's fine.. but in the HTML i get value="value"
and not value="false"
.
What's happening?
Also, you should note that if you specify a non-boolean property in the asp-for attribute of an input tag helper, Razor renders an input whose type is set to text, so you must use plain HTML to render checkboxes that enable binding to a simple collection.
What is Razor Engine In MVC 5? Razor Engine is an advanced view engine. This is not a new language but it is a new markup syntax. The namespace for Razor Engine is System.Web.Razor. View file extension is .cshtml or .vbhtml (partial and layout view) based on language. Razor syntax is easy to learn and much cleaner than Web Form syntax.
Before moving on this, Create an empty ASP.NET MVC project and whenever we create a controller it creates an empty index action. Add a view page for this index action by right clicking on Index (controller action) and adding View (Index). If you are not aware of the process to create ASP.NET MVC empty project, follow the steps 1 to 3 here.
Populate a DropdownList using Model Populate a DropdownList using Global Static Data in View Before moving on this, Create an empty ASP.NET MVC project and whenever we create a controller it creates an empty index action. Add a view page for this index action by right clicking on Index (controller action) and adding View (Index).
See this Answer
Basically you want to use HTML.CheckBoxFor(model => model.booleanProperty)
Do this
<input id="@string.Format("presentation.ServiceInfoes[{1}].Locations[{0}].Key", index, Model.Item2)" type="checkbox" @(location.ThirdValue ? "checked" : "") name="@string.Format("presentation.ServiceInfoes[{1}].Locations[{0}].ThirdValue", index, Model.Item2)" />
The value is not setting checkbox to be checked or not, value has different function. For example if you set value to 'test' and check the checkbox, when you submit the form, you will see that instead of true value of submitted variable will be 'test';
you can do pretty cool stuff with it. For example you have 3 checkboxes on your form. all of them have the same name, but different values. when you submit the form, the result you get will be comma-separated string with values of checked checkboxes;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With