Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding a list of int from hidden field to property of a model in MVC c#?

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;}
like image 552
Mohammad Dayyan Avatar asked May 20 '14 05:05

Mohammad Dayyan


People also ask

How does model binding work in MVC?

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.


1 Answers

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

like image 92
AliRıza Adıyahşi Avatar answered Nov 04 '22 21:11

AliRıza Adıyahşi