简体   繁体   English

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

[英]MCV3 Entity Framework 4 Code First: Editting Users Multiple Emails

Using MVC3 with C# and Razor as well as EF4 with code first to create a site where you can create and manipulate users. 首先将MVC3与C#和Razor以及EF4与代码一起使用,以创建一个站点,您可以在其中创建和操作用户。 Its very simple, and there are only 2 types of object: 它非常简单,只有两种类型的对象:

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; }        
}

The User / Email relationship is obviously "one to many". User / Email关系显然是“一对多”。 I have set up the CRUD functions for the User objects, and they work (mostly). 我已经为User对象设置了CRUD功能,并且它们可以正常工作。 I would like to make the edit page for the user so that all their email addresses can also be edited. 我想为用户创建编辑页面,以便也可以编辑他们的所有电子邮件地址。 (I have seen similar posts to this, they got me closer but still not quite there) (我看到过类似的帖子,它们使我离得很近,但仍然不太远)

My Edit view has working editors for all but the Emails , for which i have this: 我的“ Edit视图具有除“ Emails所有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>

Where the @Html.EditorFor(model => model.Emails) is adding ~/Views/Shared/EditorTemplates/Email.cshtml which looks like this: @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>

The result being a rendered page with text boxes for each of the users emails addresses, which looks right, but doesnt quite work. 结果是为每个用户的电子邮件地址提供了一个带有文本框的呈现页面,该页面看起来不错,但效果不佳。 here is the html: 这是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>

Which again, i thought looks about right. 再次,我以为看起来正确。 However! 然而! when the form is submitted the changes to the emails are ignored. 提交表单后,对电子邮件所做的更改将被忽略。 I guess that i am missing something from my controller, so i'll post that here too: 我想我的控制器丢失了一些内容,因此我也将其发布在这里:

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

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

You must set the state of the Email entities to Modified as well. 您还必须将“ Email实体的状态设置为“已Modified ”。 Setting the state of the user entity does not affect the related entities: 设置用户实体的状态不会影响相关实体:

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

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

Note that this won't work if your view would allow to add or remove an email for the user. 请注意,如果您的视图允许为用户添加或删除电子邮件,则此方法将无效。 The code above is only suited to modify the user's emails. 上面的代码仅适用于修改用户的电子邮件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM