Asp.net MVC 5 ModelValidation不起作用

时间:2014-02-18 23:21:46

标签: c# asp.net-mvc validation asp.net-mvc-4 html-helper

所以我有一个ViewModel,并且所有属性都通过在它们上面具有必需的属性来验证。当我提交表单时,它不会抛出任何错误消息,即需要名称字段。

我想我已经找到了问题所在。它在提交按钮中,如何让按钮点击ActionList的HttpPost方法,因为此刻它没有按下它,因此模型状态没有被验证。

查看:

<h2>@ViewBag.Title</h2>
<hr>
<div class="well">
    @using (Html.BeginForm("BookingDetails", "Booking"))
    {
        @Html.ValidationSummary(true)
        <div class="form-group">
            @Html.DisplayNameFor(m => m.FirstName)
            @Html.TextBoxFor(m => m.FirstName, new { @class = "form-control" })
            @Html.ValidationMessageFor(m => m.FirstName)
        </div>

        <div class="form-group">
            @Html.DisplayNameFor(m => m.Surname)
            @Html.TextBoxFor(m => m.Surname, new { @class = "form-control" })
            @Html.ValidationMessageFor(m => m.Surname)
        </div>
        <div class="form-group">
            @Html.DisplayNameFor(m => m.EmailAddress)
            @Html.TextBoxFor(m => m.EmailAddress, new { @class = "form-control" })
            @Html.ValidationMessageFor(m => m.EmailAddress)
        </div>
        <div class="form-group">
            @Html.DisplayNameFor(m => m.MobileNumber)
            @Html.TextBoxFor(m => m.MobileNumber, new { @class = "form-control" })
            @Html.ValidationMessageFor(m => m.MobileNumber)
        </div>
        <div class="form-group">
            @Html.DisplayNameFor(m => m.NumberOfPeople)
            @Html.TextBoxFor(m => m.NumberOfPeople, new { @class = "form-control" })
            @Html.ValidationMessageFor(m => m.NumberOfPeople)
        </div>
        <div class="form-group">
            @Html.DisplayNameFor(m => m.Date)
            @Html.TextBoxFor(m => m.Date, new { @class = "form-control" })
            @Html.ValidationMessageFor(m => m.Date)
        </div>
        <div>
            @Html.DisplayNameFor(m => m.Time)
            @Html.TextBoxFor(m => m.Time, new { @class = "form-control" })
            @Html.ValidationMessageFor(m => m.Time)
        </div>
    }
    </div>
        <div class="form-group">
            @Html.ActionLink("Book my table", "BookingDetails", "Booking" new { @class ="btn btn-primary" })
        </div>

控制器:

    // GET:
    public ActionResult BookingDetails()
    {
        return View();
    }

    // Post
    [HttpPost]
    public ActionResult BookingDetails(BookingDetailsViewModel model)
    {
        if(ModelState.IsValid)
        {
            return RedirectToAction("BookingConfirmation");
        }

        return View(model);
    }

1 个答案:

答案 0 :(得分:6)

我可以看到两个问题。

首先,您的按钮不是提交按钮。它是一个位于表单之外的链接。您必须将其移动到表单中并将其设置为提交按钮(或者编写一些在单击时提交表单的JavaScript):

    <div>
        @Html.DisplayNameFor(m => m.Time)
        @Html.TextBoxFor(m => m.Time, new { @class = "form-control" })
        @Html.ValidationMessageFor(m => m.Time)
    </div>

    <input type="submit" class="btn btn-primary" /> <!-- move it inside the form -->
    @*           ^^^^ submit    *@
} <!-- end of your form is here -->

此外,Html.ValidationSummary(true)会隐藏验证摘要中的所有属性错误。这可能不是你想要的。如果您遇到该问题,请从通话中删除true