c# – 强力类型视图中多个模型表单提交的模型绑定

前端之家收集整理的这篇文章主要介绍了c# – 强力类型视图中多个模型表单提交的模型绑定前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在表单上绑定多个模型时遇到问题.我有一个投诉表单,包括投诉信息以及一对多投诉人.我正在提交表单,但我收到绑定的错误. ModelState.IsValid总是返回false.

如果我调试和查看ModelState错误,我得到一个说法:
“EntityCollection已经被初始化了,只有调用InitializeRelatedCollection方法才能在反序列化对象图时初始化一个新的EntityCollection”.

另外,在调试时,我可以看到投诉模型由投诉人填写,因此似乎部分正在运作.

我不知道我在做什么是不可能使用默认的ModelBinder,或者如果我不是正确的方式.我似乎没有找到任何具体的例子或文件.在stackoverflow here中可以找到一个非常类似的问题,但它似乎不处理强类型的视图.

控制器代码

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)%>

分别 – 添加“投诉”.之前“投诉人[…]

编辑:

这不是一个正确的答案.留下它取消删除只是因为这可能会添加一些值,直到正确的答案弹出.

EDIT2:

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

Here写的:

The InitializeRelatedCollection(TTargetEntity) method initializes an existing EntityCollection(TEntity) that was created by using the default constructor. The EntityCollection(TEntity) is initialized by using the provided relationship and target role names.

The InitializeRelatedCollection(TTargetEntity) method is used during deserialization only.

更多信息:

Exception:

  • InvalidOperationException

Conditions:

  • When the provided EntityCollection(TEntity) is already initialized.
  • When the relationship manager is already attached to an ObjectContext.
  • When the relationship manager already contains a relationship with this name and target role.

发生两次初始化重定义选项.不幸的是,我没有明确的想法为什么.也许这个小调查将有助于别人 – 更有经验的EF.

猜你在找的C#相关文章