Home > Software design >  Update user Profile in asp.net mvc model is not valid
Update user Profile in asp.net mvc model is not valid

Time:09-25

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">Surname</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>@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">Designation</label>@Html.EditorFor(model => model.u_lastname, new { htmlAttributes = new { @class = "form-control", placeholder = @ViewBag.u_lastname } })</div>
                    </div>
                    <div class="row mt-3">
                        <div class="col-md-12"><label class="labels">Mobile Number</label><input type="text" class="form-control" placeholder="enter phone number" value=""></div>
                        <div class="col-md-12"><label class="labels">Address</label><input type="text" class="form-control" placeholder="enter address line 2" value=""></div>
                        <div class="col-md-12"><label class="labels">Country</label><input type="text" class="form-control" placeholder="enter address line 2" value=""></div>
                    </div>
                    <div class="row mt-2">
                        <div class="col-md-6"><label class="labels">City</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">Postal Code</label>@Html.EditorFor(model => model.u_lastname, new { htmlAttributes = new { @class = "form-control", placeholder = @ViewBag.u_lastname } })</div>
                    </div>
                    <hr />
                    <h6>Education Detail</h6>
                    <div class="row mt-3">
                        <div class="col-md-12"><label class="labels">Degree Name</label><input type="text" class="form-control" placeholder="enter phone number" value=""></div>
                        <div class="col-md-12"><label class="labels">Degree Level</label><input type="text" class="form-control" placeholder="enter address line 1" value=""></div>
                    </div>
                    <div class="row mt-2">
                        <div class="col-md-6"><label class="labels">Starting Date</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">Completion Date</label>@Html.EditorFor(model => model.u_lastname, new { htmlAttributes = new { @class = "form-control", placeholder = @ViewBag.u_lastname } })</div>
                    </div>
                    <hr />  
                    <div class="mt-5 text-center">
                        <input type="submit" value="Sign Up" 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>

}

CodePudding user response:

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.

CodePudding user response:

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

  • Related