Aleksander Korovin
Aleksander Korovin

Reputation: 349

The data has been cleared after post

I have edit page and controller that creates new model object and fills some data from db into this object then send a model object to view. When I click the submit button, some fields in this object have been cleared.

For example: Before:

    user_id
    name
    birth_date
    username
    password
    id_role
    email

After (Fields that are not null or empty):

    name
    username
    birth_date

The model:

public partial class Users
{
    public Users()
    {
        this.Albums = new HashSet<Albums>();
        this.History = new HashSet<History>();
        this.Country = new HashSet<Country>();
        this.Artists = new HashSet<Artists>();
        this.SelectedCountries = new List<string>();
    }
    [DisplayName("User ID")]
    public System.Guid user_id { get; set; }
    [DisplayName("Name")]
    public string name { get; set; }
    [DisplayName("Birth date")]
    public Nullable<System.DateTime> birth_date { get; set; }
    [DisplayName("Username")]
    public string username { get; set; }
    [DisplayName("Password")]
    public string password { get; set; }
    [DisplayName("Rights")]
    public System.Guid id_role { get; set; }
    [DisplayName("User Email")]
    public string email { get; set; }
    public bool isRemember { get; set; }

    public virtual ICollection<Albums> Albums { get; set; }
    public virtual ICollection<History> History { get; set; }
    public virtual Role Role { get; set; }
    public virtual ICollection<Country> Country { get; set; }
    public virtual ICollection<Artists> Artists { get; set; }
    public virtual List<string> SelectedCountries { get; set; }
}

Edit method:

    public ActionResult Edit()
    {
        if (HttpContext.User.Identity.IsAuthenticated)
        {
            var userName = HttpContext.User.Identity.Name;
            var user = db.Users.Where(x => x.username == userName).FirstOrDefault();

            ViewBag.Countries = new MultiSelectList(db.Country, "id_country", "name", user.SelectedCountries);

            return View(user);
        }
        return HttpNotFound();
    }

Edit method for handling post request:

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit(Users users)
    {
        if (ModelState.IsValid)
        {
            foreach (var country in users.SelectedCountries)
            {
                var dbCountry = db.Country.Find(new Guid(country));
                if (dbCountry != null)
                    users.Country.Add(dbCountry);
            }
            db.Entry(users).State = System.Data.Entity.EntityState.Modified;
            //There handle of string array goes
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(users);
    }

View:

<h2>Edit</h2>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)

<fieldset>
    <legend>Users</legend>
    <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>

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

    <div class="editor-label">
        @Html.LabelFor(model => model.username)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.username)
        @Html.ValidationMessageFor(model => model.username)
    </div>
    <div class="editor-label">
        @Html.Label("Country")
    </div>
    <div class="editor-field">
        @Html.DropDownList("SelectedCountries", (ViewBag.Countries as MultiSelectList), new { multiple = "multiple", @class = "chosen", style = "width: 350px;"})
    </div>
    <p>
        <input type="submit" value="Save" />
    </p>
</fieldset>
}

Thanks in advance :)

Upvotes: 0

Views: 93

Answers (2)

Simon Whitehead
Simon Whitehead

Reputation: 65087

You will only receive values that are in your form. Http is stateless..

What you need to do.. is create a ViewModel. That ViewModel is the subset of properties from your domain model that are displayed in the view. Like this:

public class UserViewModel {
    public string Name { get; set; }
    public string Username { get; set; }
    public DateTime? DateofBirth { get; set; }
}

Use this model in your view. Then, in your controller.. get the user and update the appropriate fields:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(UserViewModel viewModel) {
    var user = db.Users.Where(x => x.username == viewModel.Username).FirstOrDefault();

    user.Name = viewModel.Name;
    user.Username = viewModel.Username;
    // .. etc.

    db.SaveChanges();
}

If you are worried about all of the manual mapping involved in this, there exists frameworks to help you with that:

You are heading down a very very daunting path if you start adding hidden fields into your view. Its a maintenance nightmare and very error prone.

Upvotes: 3

user373455
user373455

Reputation: 13271

The post operation only collects the values you have in the form.

If you want the other values to proceed in your controllers post-method, you can for example, add hidden fields.

@Html.HiddenFor(x => x.HiddenPostBack)

Upvotes: 2

Related Questions