Sono nuovo in ASP.NET MVC e utilizzo della versione 5. Ho creato un modulo nel layout e non riesco a non riuscire a visualizzare errori di convalida sulla vista. Inserirà correttamente l'azione e, se il modello è valido, verrà eseguito. Se il modello non è valido, otterrò il seguente errore.ASP.NET MVC 5 Convalida modulo
Spero che qualcuno possa indicarmi la giusta direzione. Grazie in anticipo!
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
Questo è il mio modello che sto usando:
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; }
}
Questo è il codice che ho nel mio controller casa:
[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();
}
Questa è la forma che ho nel mio layout :
@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>
}
Qual è il nome file del tuo 'View'? –