将隐藏字段中的int列表绑定到MVC c#中的模型属性?

Moh*_*yan 4 javascript c# asp.net-mvc-5

我们可以将以下隐藏字段绑定到List<int>MVC中的ViewModel属性吗?

<input type="hidden" name="HiddenIntList" id="HiddenIntList" value="[1,2,3,4,5,6,7]" />
Run Code Online (Sandbox Code Playgroud)

上面隐藏的字段用javascript填充.

ViewModel属性:

public List<int> HiddenIntList {get;set;}
Run Code Online (Sandbox Code Playgroud)

Ali*_*hşi 8

不可以.您无法将复杂类型绑定到隐藏字段.你可以这样做:

@for (int i = 0; i < Model.Count; i++) {
    <input type="hidden" name="HiddenIntList" value="@Model[i]" />
}
Run Code Online (Sandbox Code Playgroud)

和控制器

public ActionResult SomeAction(List<int> HiddenIntList){...}
Run Code Online (Sandbox Code Playgroud)

点击这里