asp.net-mvc 以 mvc 形式发布数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3426018/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Posting an array in mvc form
提问by gnome
I am looking for solutions / suggestions on posting an string[]. My Model (Program), defines the target property (Levels) as a string. I can achieve this by just using FormCollection, but would prefer to pass that string[] right into the model from the post. Here are snippets:
我正在寻找有关发布字符串 [] 的解决方案/建议。我的模型(程序),将目标属性(级别)定义为字符串。我可以通过仅使用 FormCollection 来实现这一点,但更愿意将该字符串 [] 直接从帖子传递到模型中。以下是片段:
ProgramConotroller/Create
程序控制器/创建
//
// POST: /Program/Create
[HttpPost]
public ActionResult Create(Program obj)
{
if(_service.CreateProgram(obj))
{
return RedirectToAction("Index");
}
// Add data to view data, remove if not necessary
AddToViewData(obj);
return View("Create", obj);
}
View/Create
查看/创建
...
<p>
<label>Program Levels</label>
<%= Html.ValidationMessageFor(model => model.Levels) %> <br />
<% foreach (Level level in (IEnumerable)ViewData["ProgramLevels"]) { %>
<input type="checkbox" name="Levels" value="<%= level.Id %>" <%= CommonExtensions.isChecked(level.Id.ToString(), Model.Levels) %> /><%= level.Name %><br />
<% } %>
</p>
...
回答by Yuriy Faktorovich
Read about Model Binding to Arrays.
阅读模型绑定到数组。