这是我对editcar的看法:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<J2V.Models.vehicule>" %>
//code
<% using (Html.BeginForm("editcar", "Agence", FormMethod.Post, new { @class = "search_form" })) { %>
<%: Html.ValidationSummary(true) %>
<fieldset>
<legend>vehicule</legend>
// Those colonne will not be modified
<%: Html.HiddenFor(model => model.Matv) %>
<%: Html.HiddenFor(model => model.Idag) %>
<%: Html.HiddenFor(model => model.Idcat) %>
<%: Html.HiddenFor(model => model.idmarque) %>
<%: Html.HiddenFor(model => model.modele) %>
//Colonne to edit code
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
<% } %>
这是我的控制器动作:
[HttpPost]
public ActionResult editcar(Models.vehicule model)
{
if (ModelState.IsValid)
{
entity.vehicule.AddObject(model);
entity.ObjectStateManager.ChangeObjectState(model, System.Data.EntityState.Modified);
entity.SaveChanges();
return View("index", new { id = model.Idag });
}
else
return View();
}
当我点击Update
按钮时,我收到此错误:
System.InvalidOperationException:传入字典的模型项的类型为'&lt;&gt; f__AnonymousType2
1 [System.String]' but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable
1 [J2V.Models.vehicule]'。
答案 0 :(得分:2)
通常,“索引”视图的模型是IEnumerable<YourModel>
。
但是在更新之后,您正在将一个匿名对象推送到“索引”视图中; (行: return View("index", new { id = model.Idag });
)这可能是异常的原因。
您可以重定向到索引操作:
if (ModelState.IsValid)
{
entity.vehicule.AddObject(model);
entity.ObjectStateManager.ChangeObjectState(model, System.Data.EntityState.Modified);
entity.SaveChanges();
//return View("index", new { id = model.Idag });
return RedirectToAction("index", new { id = model.Idag });
}
else
return View();
希望这会有所帮助......