MCV3实体框架4代码优先:编辑用户多个电子邮件

时间:2012-03-21 14:32:53

标签: asp.net asp.net-mvc-3 entity-framework razor

首先使用带有C#和Razor的MVC3以及带代码的EF4创建一个可以创建和操作用户的站点。它非常简单,只有两种类型的对象:

public class User
{
    [Key]        
    public int Id { get; set; }
    public string User_Name { get; set; }
    public string Password { get; set; }
    public string First_Name { get; set; }
    public string Last_Name { get; set; }

    public virtual ICollection<Email> Emails { get; set; }
}

public class Email
{
    [Key]
    public int Id { get; set; }
    public string Address { get; set; }
    public User User { get; set; }        
}

User / Email关系显然是“一对多”。我已经为User对象设置了CRUD函数,它们(大多数情况下)都可以工作。我想为用户创建编辑页面,以便也可以编辑他们的所有电子邮件地址。 (我已经看过类似的帖子了,他们让我离得更近了,但还是不太合适)

我的Edit视图除Emails以外的所有人都有工作编辑,我有这个:

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

@Html.EditorFor(model => model.Emails)添加~/Views/Shared/EditorTemplates/Email.cshtml的位置如下:

@model LoginTest.Models.Email
@Html.HiddenFor(model => model.Id)

<div class="email-editor-field">
    @Html.EditorFor(model => model.Address)
    @Html.ValidationMessageFor(model => model.Address)
</div>

结果是一个呈现的页面,其中包含每个用户电子邮件地址的文本框,这看起来是正确的,但是不起作用。这是html:

<div class="editor-label">
    <label for="Emails">Email Address(es)</label>:
</div>

<div class="editor-field">

    <input data-val="true" data-val-number="The field Id must be a number." data-val-required="The Id field is required." id="Emails_0__Id" name="Emails[0].Id" type="hidden" value="1" />

    <div class="email-editor-field">
        <input class="text-box single-line" data-val="true" data-val-regex="Please enter a valid email address" data-val-regex-pattern=".+\@.+\..+" data-val-required="The Address field is required." id="Emails_0__Address" name="Emails[0].Address" type="text" value="benmiller86@gmail.com" />
        <span class="field-validation-valid" data-valmsg-for="Emails[0].Address" data-valmsg-replace="true"></span>
    </div>
    <br />

    <input data-val="true" data-val-number="The field Id must be a number." data-val-required="The Id field is required." id="Emails_1__Id" name="Emails[1].Id" type="hidden" value="2" />

    <div class="email-editor-field">
        <input class="text-box single-line" data-val="true" data-val-regex="Please enter a valid email address" data-val-regex-pattern=".+\@.+\..+" data-val-required="The Address field is required." id="Emails_1__Address" name="Emails[1].Address" type="text" value="benm@eras.co.uk" />
        <span class="field-validation-valid" data-valmsg-for="Emails[1].Address" data-valmsg-replace="true"></span>
    </div>
    <br />

    <span class="field-validation-valid" data-valmsg-for="Emails" data-valmsg-replace="true"></span>
</div>

再一次,我认为看起来是正确的。然而!提交表单时,将忽略对电子邮件的更改。我想我错过了我的控制器的东西,所以我也会在这里发布:

[HttpPost]
public ActionResult Edit(int id, User User)
{
    try
    {
        db.Entry(User).State = EntityState.Modified;
        db.SaveChanges();

        return RedirectToAction("Index");
    }
    catch
    {
        return View();
    }
}

1 个答案:

答案 0 :(得分:1)

您还必须将Email实体的状态设置为Modified。设置用户实体的状态不会影响相关实体:

if (User.Emails != null)
    foreach (var email in User.Emails)
        db.Entry(email).State = EntityState.Modified;

db.Entry(User).State = EntityState.Modified;

请注意,如果您的视图允许为用户添加或删除电子邮件,则无法使用此功能。上面的代码仅适用于修改用户的电子邮件。