我在绑定提交多个模型的表单时遇到问题。我有一份投诉表,其中包括投诉信息以及一对多投诉人。我正在尝试提交表单,但绑定时出现错误。ModelState.IsValid 始终返回 false。

如果我调试并查看 ModelState 错误,我会得到这样的信息:“EntityCollection 已经初始化。仅应在对象图反序列化期间调用 InitializeRelatedCollection 方法来初始化新的 EntityCollection”。

另外,在调试时,我可以看到投诉模型确实填充了表单提交中的投诉人,因此看起来该部分正在工作。

我不确定我正在做的事情是否无法使用默认的 ModelBinder 实现,或者我是否只是没有以正确的方式进行操作。我似乎找不到任何具体的例子或文档。stackoverflow上可以找到一个非常相似的问题 这里 但它似乎不处理强类型视图。

控制器代码:

    public ActionResult Edit(int id)
    {
        var complaint = (from c in _entities.ComplaintSet.Include("Complainants")
                     where c.Id == id
                     select c).FirstOrDefault();

        return View(complaint);
    }

    //
    // POST: /Home/Edit/5
    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Edit(Complaint complaint)
    {
        if (!ModelState.IsValid)
        {
            return View();
        }
        try
        {
            var originalComplaint = (from c in _entities.ComplaintSet.Include("Complainants")
                                     where c.Id == complaint.Id
                                     select c).FirstOrDefault();
            _entities.ApplyPropertyChanges(originalComplaint.EntityKey.EntitySetName, complaint);
            _entities.SaveChanges();
            return RedirectToAction("Index");
        }
        catch
        {
            return View();
        }
    }

查看代码(这是由创建/编辑视图调用的部分视图,这些视图也是使用投诉强类型化的):

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<ProStand.Models.Complaint>" %>

<%= Html.ValidationSummary() %>
<% using (Html.BeginForm()) {%>

<table cellpadding="0" cellspacing="0" class="table">
    <tr>
        <td>
        <label for="DateReceived">Date Received:</label>
            <%= Html.TextBox("DateReceived") %>
            <%= Html.ValidationMessage("DateReceived", "*") %>    
        </td>
        <td>
            <label for="DateEntered">Date Entered:</label>
            <%= Html.TextBox("DateEntered")%>
            <%= Html.ValidationMessage("DateEntered", "*") %>
        </td>
    </tr>
    <tr>
        <td>
            <label for="Concluded">Concluded:</label>
            <%= Html.CheckBox("Concluded")%>
            <%= Html.ValidationMessage("Concluded", "*") %>
            </td>
        <td>
            <label for="IncidentDate">Incident Date:</label>
            <%= Html.TextBox("IncidentDate")%>
            <%= Html.ValidationMessage("IncidentDate", "*") %></td>
    </tr>
</table>
    <hr />
    <table>
    <% if (Model != null) {
           int i = 0;
       foreach (var complainant in Model.Complainants){ %>
       <%= Html.Hidden("Complainants[" + i + "].Id", complainant.Id)%>
    <tr>
        <td>
            <label for="Surname">Surname:</label>

            <%= Html.TextBox("Complainants[" + i + "].Surname", complainant.Surname)%>
            <%= Html.ValidationMessage("Surname", "*")%>
        </td>
        <td>
            <label for="GivenName1">GivenName1:</label>
            <%= Html.TextBox("Complainants[" + i + "].GivenName1", complainant.GivenName1)%>
            <%= Html.ValidationMessage("GivenName1", "*")%>
        </td>
    </tr>
    <% i++; %>
    <% }} %>
    <tr>
        <td colspan=2>
            <input type="submit" value="Submit" />
        </td>
    </tr>
</table>
<% } %>
<div>
    <%=Html.ActionLink("Back to List", "Index") %>
</div>
有帮助吗?

解决方案

盲猜:

改变:

<%= Html.TextBox("Complainants[" + i + "].Surname", complainant.Surname)%>

和:

<%= Html.TextBox("Complaint.Complainants[" + i + "].Surname",  
complainant.Surname)%>

分别 - 添加“投诉”。在“投诉人[...”之前

编辑:

这不是正确的答案。不删除它只是因为这可能会增加一些价值,直到出现正确的答案。

编辑2:

我可能是错的,但对我来说,实体框架(或者 - 你使用它的方式)似乎存在问题。我的意思是 - asp.net mvc 设法从请求中读取值,但无法初始化投诉人集合。

这里 它写道:

InitializeRelatedCollection(TTargetEntity) 方法初始化使用默认构造函数创建的现有 EntityCollection(TEntity)。EntityCollection(TEntity) 使用提供的关系和目标角色名称进行初始化。

InitializeRelatedCollection(TTargetEntity) 方法仅在反序列化期间使用。

更多信息:

例外:

  • 无效操作异常

状况:

  • 当提供的 EntityCollection(TEntity) 已初始化时。
  • 当关系管理器已附加到 ObjectContext 时。
  • 当关系经理已包含具有此名称和目标角色的关系时。

不知何故,InitializeRelatedCollection 被触发两次。不幸的是 - 我没有什么好主意到底为什么。也许这个小小的调查会对其他人有所帮助 - 在 EF 方面更有经验。:)

编辑3:
这不是这个特定问题的解决方案,更像是一种解决方法,一种处理 mvc 模型部分的正确方法。

创建视图模型仅用于演示目的。也从纯 POCO 创建新的域模型(因为 EF 仅在下一个版本中支持它们)。使用 自动映射器 映射 EFDataContext<=>Model<=>ViewModel。

这需要一些努力,但应该这样处理。此方法从模型中删除了表示责任,清理了域模型(从模型中删除了 EF 内容),并解决了绑定问题。

其他提示

public ActionResult Edit([Bind(Exclude = "Complainants")]Complaint model)
{
  TryUpdateModel(model.Complainants, "Complainants");
  if (!ModelState.IsValid)
  {
      // return the pre populated model
      return View(model);
  }

}

这对我的作品!

我认为投诉对象被创建时则其“投诉人集合被初始化(因为实体框架自动逻辑的),然后模型粘合剂尝试创建集合本身,以及,这导致错误。 但是,当我们试图更新模型,然后手动收集已初始化,但模型绑定不要求再次进行初始化。

要得到这个不逐案变通方法,你需要创建自己的模型粘结剂和覆盖方法的SetProperty工作:

public class MyDefaultModelBinder : DefaultModelBinder
{
    protected override void SetProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor, object value)
    { 
        ModelMetadata propertyMetadata = bindingContext.PropertyMetadata[propertyDescriptor.Name]; 
        propertyMetadata.Model = value;
        string modelStateKey = CreateSubPropertyName(bindingContext.ModelName, propertyMetadata.PropertyName);

        // Try to set a value into the property unless we know it will fail (read-only 
        // properties and null values with non-nullable types)
        if (!propertyDescriptor.IsReadOnly) { 
        try {
            if (value == null)
            {
            propertyDescriptor.SetValue(bindingContext.Model, value);
            }
            else
            {
            Type valueType = value.GetType();

            if (valueType.IsGenericType && valueType.GetGenericTypeDefinition() == typeof(EntityCollection<>))
            {
                IListSource ls = (IListSource)propertyDescriptor.GetValue(bindingContext.Model);
                IList list = ls.GetList();

                foreach (var item in (IEnumerable)value)
                {
                list.Add(item);
                }
            }
            else
            {
                propertyDescriptor.SetValue(bindingContext.Model, value);
            }
            }

        }
        catch (Exception ex) {
            // Only add if we're not already invalid
            if (bindingContext.ModelState.IsValidField(modelStateKey)) { 
            bindingContext.ModelState.AddModelError(modelStateKey, ex); 
            }
        } 
        }
    }
}

不要忘记在Global.asax中注册您的粘合剂:

ModelBinders.Binders.DefaultBinder = new MyDefaultModelBinder();

我工作的ModelBinding异常周围通过执行以下操作:

// Remove the error from ModelState which will have the same name as the collection.
ModelState.Remove("Complaints"/*EntityCollection*/); 
if (ModelState.IsValid) // Still catches other errors.
{
    entities.SaveChanges(); // Your ObjectContext
}

的主要缺点是,该异常仍然抛出并且可以在运行时是昂贵的。优雅的解决办法可能是创建围绕现有DefaultBinder的包装,并防止再次实例化EntityCollection,这已经是由EF完成。然后结合该集合的形式值(的FormCollection)。

请记住,如果你要绑定多个集合,你将需要删除错误的每个集合。

在我的实验中,收集保存成功以及其中所述收集是一部分图中的根对象。

希望帮助别人。

我有相同的问题!最后,你会发现,该框架不能处理复杂的模型。

我写的可以初始化上的交复杂绑定一点结合成分。

但基本上你需要做的是什么就是什么阿尔尼斯L.告诉。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top