假设我有这个视图模型:
public class CreateGameViewModel
{
[Required]
public string SideA { get; set; }
[Required]
public string SideB { get; set; }
[HiddenInput(DisplayValue = false)]
public IEnumerable<int> ConfigurableCategoryIDs { get; set; }
}
这些行动:
[HttpGet]
public ActionResult Create()
{
var viewModel = new CreateGameViewModel { ConfigurableCategoryIDs = new List { 1, 1, 1 } };
return View(viewModel);
}
[HttpPost]
public ActionResult Create(CreateGameViewModel viewModel)
{
...
}
在View中我尝试编辑ConfigurableCategoryIDs值:
@for (int i = 0; i < Model.ConfigurableCategoryIDs.Count(); i++)
{
var item = Model.ConfigurableCategoryIDs.ElementAt(@i);
@Html.EditorFor(m => item)
}
CreateGameViewModel.ConfigurableCategoryIDs 返回Create(CreateGameViewModel viewModel)为null,而SideA和SideB有值。
非常感谢您的解释和解决方案! 感谢
答案 0 :(得分:0)
请尝试下一步:
型号:
public class CreateGameViewModel
{
[Required]
public string SideA { get; set; }
[Required]
public string SideB { get; set; }
[HiddenInput(DisplayValue = false)]
public List<int> ConfigurableCategoryIDs { get; set; }
}
控制器:
[HttpGet]
public ActionResult Create()
{
var viewModel = new CreateGameViewModel { ConfigurableCategoryIDs = new List<int> { 1, 1, 1 } };
return View(viewModel);
}
[HttpPost]
public ActionResult Create(CreateGameViewModel viewModel)
{
return View();
}
查看:
@model MvcApplication1.Models.CreateGameViewModel
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>CreateGameViewModel</legend>
<div class="editor-label">
@Html.LabelFor(model => model.SideA)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.SideA)
@Html.ValidationMessageFor(model => model.SideA)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.SideB)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.SideB)
@Html.ValidationMessageFor(model => model.SideB)
</div>
@for (int i = 0; i < Model.ConfigurableCategoryIDs.Count(); i++)
{
@Html.EditorFor(m => Model.ConfigurableCategoryIDs[i])
}
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}