[英]ASP.NET MVC 5 HttpPost
我有控制器:
[HttpGet]
public ActionResult ItemIndex()
{
List<Item> item = RepositoryFactory.Create<IItemRepository>().ItemList();
return View(item);
}
[HttpPost]
public ActionResult ItemIndex(FormCollection formCollection)
{
foreach( string key in formCollection.AllKeys)
{
Response.Write("Key" + key);
Response.Write(formCollection[key]);
}
return View();
}
並查看:
@model List<Kev.Models.Item>
<div style="font-family:Arial">
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
foreach (var item in Model)
{
@Html.LabelFor(m => item.Start)
@Html.EditorFor(m => item.Start)
@Html.ValidationMessageFor(m => item.Start)
}
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Zapisz" class="btn btn-default" />
</div>
</div>
}
</div>
存儲庫中具有DbContext,其中包含Item。 我想做的是在“視圖”中填充此文本框,然后提交更改以使用此值更新現有數據庫。 我現在所做的不是工作代碼,我什至無法使此HttpPost工作。 它在@foreach內彈出>> Model的NullReferenceException,並且不確定如何解決。
將您的foreach
更改為for
循環,如下所示:
for(int i=0; i<Model.Count;i++)
{
@Html.LabelFor(m => Model[i].Start)
@Html.EditorFor(m => Model[i].Start)
@Html.ValidationMessageFor(m => Model[i].Start)
}
並將操作參數形式FormCollection
更改為List<Item>
:
[HttpPost]
public ActionResult ItemIndex(List<Kev.Models.Item> model)
{
return View();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.