Have created a model and with required fields and used to create a form like so:
Model:
public class formModel {
[Required]
public string name {get;set;}
[Required]
public string Add1 {get;set;}
etc....
}
View:
@model myProj.Models.formModel
@using (BeginForm("Action", "Controller", FormMethod.Post))
{
@Html.TextBoxFor(f => f.name)
@Html.TextBoxFor(f => f.Add1)
etc...
@Html.ValidationSummary()
<button type="submit" value="submit">Submit</button>
}
Controller:
[HttpPost]
public ActionResult Action(formModel f)
{
if (ModelState.IsValid)
{
// Do Stuff here
return RedirectToAction("Result");
}
return RedirectToAction("Form", new { id = "showForm" });
}
Problem is the validation summary is being displayed if the model is in valid. Have used same approach on lots of other forms and has been fine.
Any ideas?