ASP.NET MVC 5表单验证

Bor*_*ary 8 c# asp.net validation asp.net-mvc asp.net-mvc-5

我是ASP.NET MVC的新手并使用版本5.我创建了一个布局中的表单,我不能让它在视图上显示验证错误.它将正确地发布到操作,如果模型有效,它将执行.如果模型无效,我将收到以下错误.

我希望有人能指出我正确的方向.先感谢您!

Server Error in '/' Application.

The view 'ContactSubmit' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Home/ContactSubmit.aspx
~/Views/Home/ContactSubmit.ascx
~/Views/Shared/ContactSubmit.aspx
~/Views/Shared/ContactSubmit.ascx
~/Views/Home/ContactSubmit.cshtml
~/Views/Home/ContactSubmit.vbhtml
~/Views/Shared/ContactSubmit.cshtml
~/Views/Shared/ContactSubmit.vbhtml
Run Code Online (Sandbox Code Playgroud)

这是我使用的模型:

public partial class Lead
{
    [Key]
    public int LeadId { get; set; }

    [Required]
    [StringLength(50, MinimumLength=2, ErrorMessage="* A valid first name is required.")]
    [Display(Name="First Name")]
    public string FirstName { get; set; }

    [Required]
    [StringLength(50, MinimumLength=2, ErrorMessage="* A valid last name is required.")]
    [Display(Name="Last Name")]
    public string LastName { get; set; }

    [Required]
    [StringLength(50, MinimumLength=2, ErrorMessage="* A valid company is required.")]
    public string Company { get; set; }

    [Required]
    [StringLength(50)]
    [EmailAddress(ErrorMessage="* A valid email address is required.")]
    public string Email { get; set; }

    [Required]
    [StringLength(15, MinimumLength=9, ErrorMessage="* A valid phone nunber is required.")]
    [Phone(ErrorMessage="Please enter a valid phone number.")]
    public string Phone { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

这是我家庭控制器中的代码:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult ContactSubmit(
    [Bind(Include = "FirstName, LastName, Company, Email, Phone")]
    Lead lead)
{
    try
    {
        if (ModelState.IsValid)
        {
            lead.Tenant = SessionManager.Get<Tenant>(Constants.SessionTenant);
            lead.Refferer = SessionManager.Get<string>(Constants.SessionRefferal);
            DataStoreManager.AddLead(lead);
            return RedirectToAction("SubmissionConfirmed", lead);
        }
    }
    catch (DataException /* dex */)
    {
        ModelState.AddModelError("", "Unable to perform action. Please contact us.");
        return RedirectToAction("SubmissionFailed", lead);
    }

    return View(lead);
}

[HttpGet]
public ActionResult ContactSubmit()
{
    return View();
}
Run Code Online (Sandbox Code Playgroud)

这是我在布局中的表单:

               @using (Html.BeginForm("ContactSubmit", "Home", FormMethod.Post))
                    {
                        @Html.AntiForgeryToken()
                        <fieldset>
                            <div class="editor-label">
                                @Html.LabelFor(m => m.FirstName)
                            </div>
                            <div class="editor-field">
                                @Html.EditorFor(m => m.FirstName)
                                @Html.ValidationMessageFor(m => m.FirstName)
                            </div>

                            <div class="editor-label">
                                @Html.LabelFor(m => m.LastName)
                            </div>
                            <div class="editor-field">
                                @Html.EditorFor(m => m.LastName)
                                @Html.ValidationMessageFor(m => m.LastName)
                            </div>

                            <div class="editor-label">
                                @Html.LabelFor(m => m.Company)
                            </div>
                            <div class="editor-field">
                                @Html.EditorFor(m => m.Company)
                                @Html.ValidationMessageFor(m => m.Company)
                            </div>

                            <div class="editor-label">
                                @Html.LabelFor(m => m.Email)
                            </div>
                            <div class="editor-field">
                                @Html.EditorFor(m => m.Email)
                                @Html.ValidationMessageFor(m => m.Email)
                            </div>

                            <div class="editor-label">
                                @Html.LabelFor(m => m.Phone)
                            </div>
                            <div class="editor-field">
                                @Html.EditorFor(m => m.Phone)
                                @Html.ValidationMessageFor(m => m.Phone)
                            </div>
                            <div class="masthead-button-wrapper">
                                <input class="btn btn-warning" type="submit" value="Submit" />
                            </div>
                        </fieldset>
                    }
Run Code Online (Sandbox Code Playgroud)

bra*_*der 3

您的代码中有一个错误,我一开始没有注意到。在您使用的 get 方法中 -

return View();
Run Code Online (Sandbox Code Playgroud)

这意味着您的视图不允许参数,但是当您使用时出现错误 -

return View(lead);
Run Code Online (Sandbox Code Playgroud)

在这种情况下,MVC 正在寻找具有相同名称但也接受类型参数的视图Lead,但它会失败,因为没有具有该选项的视图,并且找到的唯一一个不接受从 Get 方法看到的参数。如果没有错误,您将重定向到 -

return RedirectToAction("SubmissionConfirmed", lead);
Run Code Online (Sandbox Code Playgroud)

并且永远不需要搜索带有参数的视图,因此不会出现错误。

因此,更改视图以接受参数Lead并相应地更改您的 get 方法。

也许这会有所帮助。-

[HttpGet]
public ActionResult ContactSubmit()
{
    var lead = new Lead();
    return View(lead);
}
Run Code Online (Sandbox Code Playgroud)

并在视图中添加

@model Lead
Run Code Online (Sandbox Code Playgroud)

在顶部

编辑:如果您正在重定向,您应该知道 ModelState 在每个请求中都会被初始化,因此重定向会自动清除它。如果使用客户端验证,则必须使用其他方法来传递模型状态或更好的方法。

  • 很高兴有帮助。请务必将其标记为答案,有人可能也会为此浪费时间..:) (2认同)