简体   繁体   English

MVC4验证摘要未显示

[英]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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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