我是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
这是我正在使用的模型:
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; }
}
这是我的Home控制器中的代码:
[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();
}
这是我布局中的表格:
@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>
}
您的代码中有一个错误,我没有首先注意到。在get方法中,您正在使用-
return View();
这意味着您的视图不允许使用参数,但是当您使用错误时-
return View(lead);
在这种情况下,MVC正在寻找具有相同名称的视图,但该视图也接受Lead
类型的参数,并且由于没有具有该选项的视图并且所找到的唯一一个视图不接受从Get方法看到的参数,因此MVC失败。如果没有错误,您将重定向到-
return RedirectToAction("SubmissionConfirmed", lead);
无需搜索带有参数的View,因此不会出错。
因此,将视图更改为接受参数,Lead
并相应地更改get方法。
也许这会有所帮助。--
[HttpGet]
public ActionResult ContactSubmit()
{
var lead = new Lead();
return View(lead);
}
并在视图中添加
@model Lead
在顶部
编辑:万一因为您正在重定向,您应该知道ModelState在每个请求中都已初始化,因此重定向会自动将其清除。您必须使用其他方法来传递模型状态,或者如果使用客户端验证,则更好。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句