Can we bind the following hidden field to a List<int>
property of ViewModel in MVC ?
<input type="hidden" name="HiddenIntList" id="HiddenIntList" value="[1,2,3,4,5,6,7]" />
Above hidden field fill with javascript.
ViewModel Property :
public List<int> HiddenIntList {get;set;}
ASP.NET MVC model binding allows mapping HTTP request data with a model. It is the procedure of creating . NET objects using the data sent by the browser in an HTTP request. Model binding is a well-designed bridge between the HTTP request and the C# action methods.
No. You can't bind complex types to hidden field. You can do this as following:
@for (int i = 0; i < Model.Count; i++) {
<input type="hidden" name="HiddenIntList" value="@Model[i]" />
}
and controller
public ActionResult SomeAction(List<int> HiddenIntList){...}
Check HERE
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