Hỏi đáp
Chia sẻ kiến thức, cùng nhau phát triển
Gặp lỗi update dữ liệu vào database
13:30 11-08-2017
1.497 lượt xem
1 bình luận
07:08 12-08-2017
Controller:
[HttpGet]
public ActionResult Edit(int id)
{
var user = new userDao().ViewDetail(id);
return View(user);
}
[HttpPost]
public ActionResult Edit(User user)
{
if (ModelState.IsValid)
{
var dao = new userDao();
var encryptedMD5Pas = Encryptor.MD5Hash(user.password);
user.password = encryptedMD5Pas;
var result = dao.Update(user);
if (result)
{
return RedirectToAction("Index", "User");
}
else
{
ModelState.AddModelError("", "Cập nhật user thành công");
}
// return View(result);
}
return View("View");
}
Model:
public bool Update(User entity)
{
try
{
var user = db.Users.Find(entity.user_id);
user.name = entity.name;
user.password = entity.password;
user.time_create = entity.time_create;
user.status = entity.status;
user.email = entity.email;
user.time_update = DateTime.Now;
db.SaveChanges();
return true;
}
catch(Exception ex)
{
return false;
}
}
view:
@model Model.EF.User
@using PagedList;
@using PagedList.Mvc;
@{
ViewBag.Title = "Index";
Layout = "~/Areas/Admin/Views/Shared/_Layout.cshtml";
}
@section header{
Cập nhật người dùng
}
<div class="row">
<div class="col-lg-12">
<div class="panel panel-default">
<div class="panel-heading">
Nhập thông tin
</div>
<div class="panel-body">
<div class="row">
<div class="col-lg-6">
@using (Html.BeginForm("Edit", "User", FormMethod.Post))
{
<form role="form">
@Html.AntiForgeryToken()
<h4>Cập nhật thông tin người dùng</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
<label>tên tài khoản(Không chỉnh sửa)</label>
<div class="col-md-10">
@Html.TextBoxFor(model => model.username)
@Html.ValidationMessageFor(model => model.username, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<label>Cập nhật mật Khẩu</label>
<div class="col-md-10">
@Html.PasswordFor(model => model.password, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.password, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<label>Cập nhật họ tên người dùng</label>
<div class="col-md-10">
@Html.TextBoxFor(model => model.name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.name, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<label>Cập nhật email người dùng</label>
<div class="col-md-10">
@Html.TextBoxFor(model => model.email, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.email, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Cập nhật" class="btn btn-default" />
</div>
</div>
</form>
}
</div>
</div>
</div>
</div>
</div>
</div>
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@*@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}*@
Em có chạy thì báo lỗi này:
The model item passed into the dictionary is of type 'System.Boolean', but this dictionary requires a model item of type 'Model.EF.User'.
Có ai rành asp.net MVC chỉ dùm em lỗi ở đâu không ạ
Boolean không thể ép thành User
vấn đề của bạn là do bên controller là 1 kiểu mà bên view là 1 kiểu. 2 thằng cần đồng nhất kiểu model truyền và nhận