English 中文(简体)
ASP.NET MVC 3 - EF 4.2 Code-first - Master-detail edited
原标题:ASP.NET MVC 3 - EF 4.2 code-first - master-detail editing

I m stumped with a problem in ASP.NET MVC 3 - handling editing and updating of a master-detail relationship from a single MVC view.

I m using Entity Framework 4.2 code-first to build a small app. It uses a Person class (which has Name, FirstName and so on), and an Address class (with Street, Zipcode, City etc.).

These two classes have a m:n relationship to one another, modelled in EF code-first with:

public class Person
{
    private List<Address> _addresses;

    // bunch of scalar properties like ID, name, firstname

    public virtual List<Address> Addresses
    {
        get { return _addresses; }
        set { _addresses = value; }
    }
}

public class Address
{
    private List<Person> _people;

    // bunch of scalar properties like AddressID, street, zip, city

    public virtual List<Person> People
    {
        get { return _people; }
        set { _people = value; }
    }
}

我用一些数据人工填充数据库,用EF4.2进行细微工作,检索数据。

我有一个伙伴关系。 NET MVC PersonController,其中我装上了Person,包括其所有现有地址,并用Razor的观点显示。 d 允许用户改变或更新现有地址,并删除或插入地址。 管制人员从妇女和家庭服务处获得有关该人士的数据:

public ActionResult Edit(int id)
{
    Person person = _service.GetPerson(id);
    return View(person);
}

在回程时适当填满个人地址。

对这一作品的细表——它显示了预期的<代码>Person的所有数据。

@model DomainModel.Person

<h2>Edit</h2>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Person</legend>

        @Html.HiddenFor(model => model.PersonId)

        <div class="editor-label">
            @Html.LabelFor(model => model.Name)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Name)
            @Html.ValidationMessageFor(model => model.Name)
        </div>

        .... and a few more of those scalar properties being edited....  

        <hr/>
        <h4>Addresses</h4>

        @foreach (var item in Model.Addresses)
        {
            <tr>
                <td>
                    @Html.EditorFor(modelItem => item.Street)
                </td>
                <td>
                    @Html.EditorFor(modelItem => item.ZipCode)
                </td>
                <td>
                    @Html.EditorFor(modelItem => item.City)
                </td>
            </tr>
        }
        <hr/>
        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>
}

然而,当Im试图在挽救被编辑的人时处理《持久性有机污染物手册》时,我看不到所展示的(可能经过编辑的)地址:

    [HttpPost]
    public ActionResult Edit(Person person)
    {
        if (ModelState.IsValid)
        {
            _service.UpdatePerson(person);
            return RedirectToAction("Index");
        }
        return View(person);
    } 

The person.Addresses property is always null in this case. Hmmm.... what am I missing?? How can I edit a Person-to-Addresses relationship on an ASP.NET MVC view, and get back the Person AND its associated Addresses from my view, so I can pass them to EF to save them back to the database tables??

最佳回答

我怀疑,地址的投入领域有错的名称。

我建议你使用编辑样板。 页: 1 参看Razor的观点,其中一项呼吁是:EditorFor,帮助地址财产:

<h4>Addresses</h4>

@Html.EditorFor(x => x.Addresses)

<hr/>

并且现在界定了该地址的编辑模板,该模板将自动提供给收集的每一部分(~/Views/comm/EditorTemplates/Address.cshtml):

@model Address
<tr>
    <td>
        @Html.EditorFor(x => x.Street)
    </td>
    <td>
        @Html.EditorFor(x => x.ZipCode)
    </td>
    <td>
        @Html.EditorFor(x => x.City)
    </td>
</tr>

如今,在您提交时,您的个人模式上的<代码>附加物财产将受到正确的约束。

说明:编辑模板也可放在<代码>~/Views/XXX/EditorTemplates/Address.cshtml内,其中三十位是目前的控制器。 在这种情况下,只能根据现任控制者的意见加以重复使用,而不是使其在全球可见(通过分享)。

关于违约模型的无线格式如何约束的更深入的概述,请参看

问题回答

我认为,你通过将每个区块改成以下的区块,取得了预期的效果:

    @for (var i = 0; i < Model.Addresses.Count(); i++)
    {
        <tr>
            <td>
                @Html.EditorFor(model => model.Addresses[i].Street)
            </td>
            <td>
                @Html.EditorFor(model => model.Addresses[i].ZipCode)
            </td>
            <td>
                @Html.EditorFor(model => model.Addresses[i].City)
            </td>
        </tr>
    }

这当然要求按指数可查到地址。

其原因是,在<代码>foreach。 由此而产生的障碍是:

<input type="text" name="Street" value="Test Street 1" />

<input type="text" name="Addresses[0].Street" value="Test Street 1" />

模型约束器然后使用表格现场名称与模型类别特性相匹配。

两个例子都简化,以达到点数,只有100%正确。





相关问题
Performance impact of indexed view in MS SQL Server 2008

Does anyone have experience with using indexed view in MS SQL Server 2008? I am trying to find out how does indexed view affect performance of insert / update statements, that are adding / updating ...

Lock Escalation - What s happening here?

While altering a table (removing a column) in SQL Server 2008, I clicked the Generate Change Script button and I noticed that the change script it generated drops the column, says "go" and then runs ...

Round to nearest 5 in SQL Server

I have a Money column in my SQL Server 2008 table. In my below query how can I round it to nearest 5$ select FineAmount from tickets Thanks

热门标签