如果ModelState无效,则在View上呈现ViewModel错误

时间:2016-10-27 14:49:53

标签: c# asp.net-mvc wpf asp.net-mvc-4 mvvm

我的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中设置的错误消息)该怎么办?

我只是做`返回View();要么 ??

我现在需要在我的视图中呈现这些消息......

2 个答案:

答案 0 :(得分:0)

每当我收到提交的无效表单时,我都会返回View(),以便他们更正问题。将他们带到一个错误页面,他们必须回到表单并重新开始会让用户感到沮丧。给他们回复无效的表格并告诉他们需要纠正什么。

现在,可以从ViewBag()读取需要更正的内容。或者你可以在你内部Model一些属性,这些属性将保存用户的错误消息,如果它们不为空则显示它们。

答案 1 :(得分:0)

如果模型状态无效,您只需将模型作为参数返回当前视图:

if (!ModelState.IsValid)
{
    return View(model);
}

编辑:在你的html中,添加html元素以显示验证消息:

@Html.ValidationMessageFor(model => model.FirstName)