简体   繁体   中英

MVC4 validation summary not showing

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?

When the model is invalid, do not use

return RedirectToAction("Form");

But

return View(f); // or return View("ViewName", f);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM