简体   繁体   中英

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. 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". I have set up the CRUD functions for the User objects, and they work (mostly). 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:

<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:

@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:

<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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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