【发布时间】: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" 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>
}
【问题讨论】:
-
什么时候告诉你模型无效?当您检查模型状态时,它是否在您的
EditProfile方法中? -
另外,
Userdb对象的唯一标识符是什么? -
你必须展示你对这个模型的看法。我很期待。
-
@AnnL。是的,当我单击按钮时,它没有更新数据,所以我使用断点检查,模型无效
标签: c# sql asp.net asp.net-mvc entity-framework