这是我的模型:
public class MyModel
{
public List<long> NeededIds { get; set; }
public string Name { get; set; }
}
我的控制器:
public ActionResult Create()
{
MyModel model = new MyModel();
model.NeededIds = new List<long> { 1, 2, 3, 4 };
return View(model);
}
[HttpPost]
public ActionResult Create(MyModel model)
{
string name = model.Name;
List<long> ids = model.NeededIds;
return RedirectToAction("Index");
}
并查看:
@model TestMVC.Models.MyModel
@using(Html.BeginForm()) {
<table>
<thead>
<tr>
<th>
Id
</th>
</tr>
</thead>
<tbody>
@foreach(long id in Model.NeededIds) {
<tr>
<td>
@id
</td>
</tr>
}
</tbody>
</table>
@Html.ValidationSummary(true)
<fieldset>
<legend>MyModel</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
我NeededIds
在 Get action 中设置,在我可以看到的视图中NeededIds
。我在发布操作中也需要它,但在发布操作NeededIds
中始终为空。当我在获取操作中设置属性值时,如何在发布操作中获取属性值?你的建议是什么?