MVC4 C#multiple形成一页验证

时间:2013-09-27 11:07:08

标签: asp.net-mvc templates entity-framework-4 html-helper

我在mvc项目的一个视图中有3个表单,如“

查看:

 <div>
        <fieldset>            
            <legend>Deposit Money</legend>
            <div>@Html.LabelFor(u=>u.AccountNumber1)</div>
            <div>@Html.DropDownList("Accounts",  "-- Select Account --")
            </div>

            <div>@Html.LabelFor(u=>u.Amount)</div>
            <div>@Html.TextBoxFor(u => u.Amount,new {style = "width:150px"})
                @Html.ValidationMessageFor(u => u.Amount)
            </div>

            <div>@Html.LabelFor(u=>u.Comment)</div>
            <div>@Html.TextAreaFor(u => u.Comment,new {style = "width:250px"})
                @Html.ValidationMessageFor(u => u.Comment)
            </div>

            <input type="submit" value ="Submit" />
            <input type="reset" value ="Clear" />

        </fieldset>

    </div>

}
</div> 
<div id="middlepanel" style="position:absolute;left:33%;right:33%;">
    @using (Html.BeginForm("Withdrawal", "ATM", FormMethod.Post, new { })) 
{
    @Html.ValidationSummary(true,"Deposit Failed. Check Your Details");

    <div>
        <fieldset>
            <legend>Withdraw Money</legend>
            <div>@Html.LabelFor(u=>u.AccountNumber1)</div>
            <div>@Html.DropDownList("Accounts",  "-- Select Account --")
            </div>

            <div>@Html.LabelFor(u=>u.Amount)</div>
            <div>@Html.TextBoxFor(u => u.Amount,new {style = "width:150px"})
                @Html.ValidationMessageFor(u => u.Amount)
            </div>

            <div>@Html.LabelFor(u=>u.Comment)</div>
            <div>@Html.TextAreaFor(u => u.Comment,new {style = "width:250px"})
                @Html.ValidationMessageFor(u => u.Comment)
            </div>

            <input type="submit" value ="Submit" />
            <input type="reset" value ="Clear" />

        </fieldset>

    </div>

}
</div> 
<div id="rightpanel" style="position:absolute;right:0;width:33%;">
    @using (Html.BeginForm("Transfer", "ATM", FormMethod.Post, new { })) 
{
    @Html.ValidationSummary(true,"Deposit Failed. Check Your Details");

    <div>
        <fieldset>
            <legend>Transfer Money</legend>
            <div>@Html.LabelFor(u=>u.AccountNumber1)</div>
            <div>@Html.DropDownList("Accounts",  "-- Select Account --")
            </div>
            <div>@Html.LabelFor(u=>u.AccountNumber2)</div>
            <div>@Html.DropDownList("Accounts",  "-- Select Account --")
            </div>

            <div>@Html.LabelFor(u=>u.Amount)</div>
            <div>@Html.TextBoxFor(u => u.Amount,new {style = "width:150px"})
                @Html.ValidationMessageFor(u => u.Amount)
            </div>

            <div>@Html.LabelFor(u=>u.Comment)</div>
            <div>@Html.TextAreaFor(u => u.Comment,new {style = "width:250px"})
                @Html.ValidationMessageFor(u => u.Comment)
            </div>

            <input type="submit" value ="Submit" />
            <input type="reset" value ="Clear" />

        </fieldset>

    </div>

}
</div>

在我的控制器中,我正在处理上述每个功能。

但是当其中一个表单出错时(即空白字段​​) 错误显示在所有表单上(validationSummary)

我如何为每个表单构建隐藏错误?

1 个答案:

答案 0 :(得分:0)

考虑为每个表单使用部分视图,并使用

在主视图中调用部分视图
@Html.Partial("PartialViewName")