在 asp.net mvc 模型中更新用户配置文件无效
Posted
技术标签:
【中文标题】在 asp.net mvc 模型中更新用户配置文件无效【英文标题】:Update user Profile in asp.net mvc model is not valid 【发布时间】:2021-11-17 07:12:33 【问题描述】:我是 ASP.NET MVC 的初学者。
我想更新登录用户配置文件,但遇到了问题。我的代码说模型状态无效。我已经尝试了所有我知道并在 Google 上搜索过的可能解决方案。
我正在使用实体框架,下面是我的代码。
表名:Userdb
在控制器中获取action方法:
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);
控制器中的发布操作方法:
[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();
查看
@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" 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>
【问题讨论】:
什么时候告诉你模型无效?当您检查模型状态时,它是否在您的EditProfile
方法中?
另外,Userdb
对象的唯一标识符是什么?
你必须展示你对这个模型的看法。我很期待。
你需要learn about AutoMapper!!
@AnnL。是的,当我单击按钮时,它没有更新数据,所以我使用断点检查,模型无效
【参考方案1】:
您的视图存在多个问题,这可能导致模型无效。如果您发布 Userdb
的定义,并附上任何元数据类(如果仍在使用),我们可以肯定地看到。
首先,您没有将必要的name
属性放在所有普通的input
字段中。这意味着它们不会作为数据发布到服务器,因此模型绑定器将无法自动填充模型中的这些字段。
其次,您在多个地方使用了@Html.EditorFor()
,并引用了相同的属性。这意味着您在标有“姓名”的文本框中输入的内容将被分配到模型上的 u_Firstname 字段 - 但您在“性别”和“城市”中输入的内容也会如此领域!
这意味着您的模型上的实际 Gender 和 City 属性不会被填充。
第三,你说 u_Email 是主键,但它甚至没有从这个表单回传。
这让我怀疑问题的真正根源在于:您的模型在发布时没有从表单中完全填充,因为您没有收集所有数据。而且我猜至少其中一些属性有Required
或其他限制,这意味着不允许将它们留空。 (u_Email 可能是候选者,因为它是主键。)
如果您在调试时查看editEntity
的内容,可能会非常清楚哪些已填充,哪些未填充。
【讨论】:
非常感谢您的详细审查。 1. 我更新了视图 2. 剩下的一切都很好,所有必填字段都填写,但仍然得到无效的 ModelState。 3. EditEntity 正在填充我想要更改的所有值等 您说“editEntity 正在填充我想要更改的所有值”。但它是否填充了您需要的所有值,包括电子邮件地址?请记住,您传递的应该是有效的Userdb
对象,适合保存,即使您只是将其用作数据传输对象。
您的表单没有输入字段,我可以看到它映射到 u_Email
,即使您正在显示它(spans
不要回发数据),所以它看起来对我来说就像editEntity
对象不会有填充的u_Email
,我相信这足以让模型被视为无效。
解决此问题的一种方法,并判断问题的真正原因:创建(临时)一个较小的对象,仅包含您要更改的字段,没有验证属性并且不参与 EF。称它为UserViewModel
。
为你的模型创建一个UserViewModel
的实例,并来回传递它,像现在一样进行值复制,但在Userdb
EF 对象和模型之间。看看这是否会使验证错误消失。【参考方案2】:
替换此代码
db.Entry(model).State = EntityState.Modified;
与
db.Entry(model).State = System.Data.Entity.EntityState.Modified;
上面的整个代码是正确的,但主要问题是当你改变实体的状态时,EntityState.Modified
应该替换为System.Data.Entity.EntityState.Modified;
,因为EntityState
是System.Data.Entity*
命名空间的对象
【讨论】:
那会有什么帮助? 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center。以上是关于在 asp.net mvc 模型中更新用户配置文件无效的主要内容,如果未能解决你的问题,请参考以下文章
模型没有在 asp.net mvc 中使用 ajax post 更新?