如果ModelState无效,则在视图上呈现ViewModel错误
本文关键字:ViewModel 错误 视图 ModelState 无效 如果 | 更新日期: 2023-09-27 18:15:11
在我的MVC应用程序中有一个像下面这样的表单:
@using (Html.BeginForm("Register", "User", FormMethod.Post))
{
<div>
@Html.TextBoxFor(m => m.FirstName, new { placeholder = "First name", @class = "form-control", @type = "text" })
</div>
<div>
@Html.TextBoxFor(m => m.LastName, new { placeholder = "Last name", @class = "form-control", @type = "text" })
</div>
<div>
@Html.TextBoxFor(m => m.Email, new { placeholder = "Email", @class = "form-control", @type = "email" })
</div>
<div>
@Html.TextBoxFor(m => m.Password, new { placeholder = "Password", @class = "form-control", @type = "password" })
</div>
<div>
@Html.TextBoxFor(m => m.PasswordConfirm, new { placeholder = "Confirm password", @class = "form-control", @type = "password" })
</div>
<div>
@Html.DropDownListFor(model => model.SelectedCountryId, Model.Countries, new { @class="select2_single form-control select2-hidden-accessible", @tabindex = "-1" })
</div>
<div>
<input class="btn btn-default submit" type="submit" value="Register" />
</div>
}
我的ViewModel看起来像这样:
public class UserRegistrationViewModel
{
[Required(ErrorMessage = "First name is required!")]
public string FirstName { get; set; }
[Required(ErrorMessage = "Last name is required!")]
public string LastName { get; set; }
[Required(ErrorMessage = "Email name is required!")]
public string Email { get; set; }
[Required(ErrorMessage = "Password name is required!")]
public string Password { get; set; }
[Required(ErrorMessage = "Password confirmation name is required!")]
public string PasswordConfirm { get; set; }
public int SelectedCountryId { get; set; }
[Required(ErrorMessage = "Country needs to be selected!")]
public SelectList Countries { get; set; }
}
这是我的两个动作:
public ActionResult Index()
{
var model = new UserRegistrationViewModel();
var countries = Connection.ctx.Countries.OrderBy(x => x.CountryName).ToList();
model.Countries = new SelectList(countries, "CountryId", "CountryName");
return View(model);
}
[HttpPost]
public ActionResult Register(UserRegistrationViewModel model)
{
if (ModelState.IsValid)
{
var user = new Users();
user.FirstName = model.FirstName;
user.LastName =model.LastName;
user.Email = model.Email;
user.PasswordSalt = Helpers.PasswordHelper.CreateSalt(40);
user.PasswordHash = Helpers.PasswordHelper.CreatePasswordHash(model.Password, user.PasswordSalt);
user.CountryId = Convert.ToInt32(model.SelectedCountryId);
user.Active = true;
Connection.ctx.Users.Add(user);
Connection.ctx.SaveChanges();
var role = new UserRoles();
role.RoleId = 2;
role.UserId = user.UserId;
role.Active = true;
user.UserRoles.Add(role);
Connection.ctx.SaveChanges();
return RedirectToAction("Index");
}
return null;
}
现在我的问题是,如果模型状态无效,我该怎么办?显示错误信息,我已经设置在我的ViewModel)??
我只是做' return View();还是? ?
我现在需要在我的视图中呈现这些消息
每当我收到提交的无效表单时,我返回View()
以纠正问题。将用户带到一个错误页面,他们必须返回表单并重新开始,这会让用户感到沮丧。把无效的表格还给他们,并告诉他们哪些地方需要修改。
现在,需要校正的可以从ViewBag()
中读取。或者你可以在你的Model
内部有一些属性,将为用户保存错误信息,并在它们不为空时显示它们。
在无效模型状态的情况下,您可以只返回当前视图,并将模型作为参数:
if (!ModelState.IsValid)
{
return View(model);
}
编辑:在你的html中,添加html元素来显示验证消息:
@Html.ValidationMessageFor(model => model.FirstName)