1

这是我对 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:传入字典的模型项的类型为“<> f__AnonymousType2 1 [System.String]' but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable1 [J2V.Models.vehicule]”。

4

1 回答 1

2

通常,“索引”视图的模型是IEnumerable<YourModel>. 但是在更新之后,您将一个匿名对象推送到“索引”视图中;(行:)return View("index", new { id = model.Idag });这可能是异常的原因。

您可以重定向到 index 操作:

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();

希望这可以帮助...

于 2012-04-12T16:57:27.777 回答