skip to Main Content

I’m a beginner to ASP.NET MVC.

I want to update login user profile and I am having a problem. my code says model state is not valid. I’ve tried every possible solution which I know and also searched on Google.

I’m using Entity Framework and below is my code.

Table name: Userdb

Get action method in the controller:

public ActionResult EditProfile()
{
    string username = User.Identity.Name;

    Userdb user = db.Userdbs.FirstOrDefault(u => u.u_Email.Equals(username));
           
    Userdb model = new Userdb();

    // Personal Details
    model.u_Firstname = user.u_Firstname;
    model.u_lastname = user.u_lastname;
    model.u_dob = user.u_dob;
    model.u_mobile = user.u_mobile;
    model.u_title = user.u_title;

    // this to display data in texbox
    ViewBag.u_Email = user.u_Email;
    ViewBag.u_Firstname = user.u_Firstname;
    ViewBag.u_lastname = user.u_lastname;
    ViewBag.u_dob = user.u_dob;
    ViewBag.u_mobile = user.u_mobile;
    ViewBag.u_title = user.u_title;

    // Education Details
    model.Degree_level = user.Degree_level;
    model.Degree_name = user.Degree_name;
    model.Starting_date = user.Starting_date;
    model.Completion_date = user.Completion_date;  

    // Address Details 
    model.country_name = user.country_name;
    model.city_name = user.city_name;
    model.Address = user.Address;
    model.postal_code = user.postal_code;    

    // Social Link Details
    model.S_fb_url = user.S_fb_url;
    model.S_linkedin_url = user.S_linkedin_url;
    model.S_github_url = user.S_github_url;
    model.S_twitter_url = user.S_twitter_url;

    return View(model);
}

Post action method in controller:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult EditProfile(Userdb editEntity)
{
    if (ModelState.IsValid)
    {
        string username = User.Identity.Name;

        // Get the userprofile
        Userdb model =new Userdb();

        // Personal Details
        model.u_Firstname = editEntity.u_Firstname;
        model.u_lastname = editEntity.u_lastname;
        model.u_dob = editEntity.u_dob;
        model.u_mobile = editEntity.u_mobile;
        model.u_title = editEntity.u_title;
        ViewBag.u_Email = editEntity.u_Email;
        ViewBag.u_Firstname = editEntity.u_Firstname;
        ViewBag.u_lastname = editEntity.u_lastname;
        ViewBag.u_dob = editEntity.u_dob;
        ViewBag.u_mobile = editEntity.u_mobile;
        ViewBag.u_title = editEntity.u_title;

        // Education Details
        model.Degree_level = editEntity.Degree_level;
        model.Degree_name = editEntity.Degree_name;
        model.Starting_date = editEntity.Starting_date;
        model.Completion_date = editEntity.Completion_date;

        // Address Details
        model.country_name = editEntity.country_name;
        model.city_name = editEntity.city_name;
        model.Address = editEntity.Address;
        model.postal_code = editEntity.postal_code;

        // Social Link Details
        model.S_fb_url = editEntity.S_fb_url;
        model.S_linkedin_url = editEntity.S_linkedin_url;
        model.S_github_url = editEntity.S_github_url;
        model.S_twitter_url = editEntity.S_twitter_url;

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

        db.SaveChanges();

        return RedirectToAction("Index", "Home"); // or whatever
    }
          
    return View();
}

View

  @model Applicant.Models.Userdb
@{
    ViewBag.Title = "EditProfile";
    Layout = "~/Views/Shared/_ProfileLayout.cshtml";
}

    @using (Html.BeginForm("EditProfile", "Users"))
    {
@Html.AntiForgeryToken()
<body>
    <div class="container rounded bg-white mt-5 mb-5">
        <div class="row">
            <div class="col-md-3 border-right">
                <div class="d-flex flex-column align-items-center text-center p-3 py-5"><img class="rounded-circle mt-5" width="150px" src="https://st3.depositphotos.com/15648834/17930/v/600/depositphotos_179308454-stock-illustration-unknown-person-silhouette-glasses-profile.jpg"><span class="font-weight-bold">@ViewBag.u_Firstname @ViewBag.u_lastname</span><span class="text-black-50">@ViewBag.u_Email</span><span> </span></div>
            </div>
            <div class="col-md-5 border-right">
                <div class="p-3 py-5">
                    <div class="d-flex justify-content-between align-items-center mb-3">
                        <h4 class="text-right">Profile Settings</h4>
                    </div>
                    <h6>Personal Detail</h6>
                    <div class="row mt-2">
                        <div class="col-md-6"><label class="labels">Name</label>@Html.EditorFor(model => model.u_Firstname, new { htmlAttributes = new { @class = "form-control", placeholder = @ViewBag.u_Firstname } })</div>
                        <div class="col-md-6"><label class="labels">Lastname</label>@Html.EditorFor(model => model.u_lastname, new { htmlAttributes = new { @class = "form-control", placeholder = @ViewBag.u_lastname } })</div>
                    </div>
                    <div class="row mt-2">
                        <div class="col-md-6">
                            <label class="labels">Gender</label><br />
                            @Html.RadioButtonFor(model => model.u_Gender, "F")
                            Female
                            @Html.RadioButtonFor(model => model.u_Gender, "M")
                            Male
                        </div>
                        <div class="col-md-6"><label class="labels">Designation</label>@Html.EditorFor(model => model.u_title, new { htmlAttributes = new { @class = "form-control", placeholder = @ViewBag.u_title } })</div>
                    </div>
                    <div class="row mt-3">
                        <div class="col-md-12"><label class="labels">Mobile Number</label>@Html.EditorFor(model => model.u_mobile, new { htmlAttributes = new { @class = "form-control", placeholder = @ViewBag.u_mobile } })</div>
                        <div class="col-md-12"><label class="labels">Address</label>@Html.EditorFor(model => model.Addres, new { htmlAttributes = new { @class = "form-control", placeholder = @ViewBag.Addres } })</div>
                        <div class="col-md-12"><label class="labels">Country</label>@Html.EditorFor(model => model.country_name, new { htmlAttributes = new { @class = "form-control", placeholder = @ViewBag.country_name } })</div>
                    </div>
                    <div class="row mt-2">
                        <div class="col-md-6"><label class="labels">City</label>@Html.EditorFor(model => model.city_name, new { htmlAttributes = new { @class = "form-control", placeholder = @ViewBag.city_name } })</div>
                        <div class="col-md-6"><label class="labels">Postal Code</label>@Html.EditorFor(model => model.postal_code, new { htmlAttributes = new { @class = "form-control", placeholder = @ViewBag.postal_code } })</div>
                    </div>
                    <hr />
                    <h6>Education Detail</h6>
                    <div class="row mt-3">
                        <div class="col-md-12"><label class="labels">Degree Name</label>@Html.EditorFor(model => model.Degree_name, new { htmlAttributes = new { @class = "form-control", placeholder = @ViewBag.Degree_name } })</div>
                        <div class="col-md-12"><label class="labels">Degree Level</label>@Html.EditorFor(model => model.Degree_level, new { htmlAttributes = new { @class = "form-control", placeholder = @ViewBag.Degree_level } })></div>
                    </div>
                    <div class="row mt-2">
                        <div class="col-md-6"><label class="labels">Starting Date</label>@Html.EditorFor(model => model.Starting_date, new { htmlAttributes = new { @class = "form-control", placeholder = @ViewBag.Starting_date } })</div>
                        <div class="col-md-6"><label class="labels">Completion Date</label>@Html.EditorFor(model => model.Completion_date, new { htmlAttributes = new { @class = "form-control", placeholder = @ViewBag.Completion_date } })</div>
                    </div>
                    <hr />  
                    <div class="mt-5 text-center">
                        <input type="submit" value="Save Changes" class="btn" style="background-color:#5777ba; color:#fff;" />
                    </div>
                </div>
            </div>
            <div class="col-md-4">
                <div class="py-5">
                    <h6>Skills Detail</h6>
                    <div class="col-md-12"><label class="labels">Skills</label><input type="text" class="form-control" placeholder="experience" value=""></div>
                </div>
                <div class="">
                    <h6>Skills Detail</h6>
                    <div class="col-md-12"><label class="labels">Github Link</label><input type="text" class="form-control" placeholder="experience" value=""></div>
                    <div class="col-md-12"><label class="labels">Linkedin Link</label><input type="text" class="form-control" placeholder="experience" value=""></div>
                    <div class="col-md-12"><label class="labels">Twitter Link</label><input type="text" class="form-control" placeholder="experience" value=""></div>
                    <div class="col-md-12"><label class="labels">Facebook Link</label><input type="text" class="form-control" placeholder="experience" value=""></div>
                </div>
            </div>
        </div>
    </div>
</body>

}

2

Answers


  1. replace this code

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

    with

    db.Entry(model).State = System.Data.Entity.EntityState.Modified;
    

    the above whole code is true but the main problem is when you change the state of entity EntityState.Modified should be replaced with System.Data.Entity.EntityState.Modified; because EntityState is an object of System.Data.Entity* namespace

    Login or Signup to reply.
  2. There are several problems with your view, which could account for the invalid model. If you post the definition of Userdb, complete with any metadata class (if those are still being used) we could see for sure.

    First, you aren’t putting the necessary name attribute in all your plain input fields. This means they won’t get posted to the server as data, so the model binder won’t be able to automatically populate those fields in your model.

    Second, you’ve used @Html.EditorFor() in several places, with the identical properties being referenced. This means that what you put in the text box labeled "Name" will be assigned to the u_Firstname field on your model — but so will what you’ve put in the "Gender" and "City" fields!
    That means that the actual Gender and City properties on your model won’t be populated.

    Third, you say u_Email is the primary key, but it isn’t even posted back from this form.

    This brings me to what I suspect the real source of the problem is: your model isn’t being fully populated from the form on posting, because you’re not collecting all the data. And I’d guess at least some of those properties have Required or other constraints on them that mean leaving them empty isn’t allowed. (u_Email is a likely candidate, since it’s the primary key.)

    If you look at the contents of editEntity while debugging, it will probably be made very clear what’s populated and what isn’t.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search