当用户选择超过5的项目时抛出错误消息

时间:2011-02-21 22:49:24

标签: asp.net-mvc-3

我有以下视图,当用户尝试使用大于5的项目提交时,我需要显示错误消息。我可以在控制器端执行此操作但是我可以在视图中使用jquery来执行此操作吗?我认为jquery会更容易,更快。

这是我的代码:

    @using (Html.BeginForm())
{
    <h1>Manage Testimonials</h1>
    <p><b>Please select up to 5 testimonials you want to include for your sales flyers. A maximum of only 5 testimonials is allowed.</b></p>
<div>
<table width="100%" cellpadding="0" cellspacing="0" rules="all">
    <thead>
        <tr>
            <td align="center" style="padding: 2px 0 2px 2px;">Select</td>
            <td align="center" style="padding: 2px 0 2px 2px;">First Name</td>
            <td align="center" style="padding: 2px 0 2px 2px;">Last Name</td>
            <td align="center" style="padding: 2px 0 2px 2px;">Testimonial</td>
        </tr>
    </thead>
    @{ var i = 0;
}
    @foreach (var testimonials in Model.Testimonials)
    {
    <tr>
        <td style="padding: 2px 0 2px 2px;">
            @Html.CheckBox("Testimonials[" + i.ToString() + "].DisplayTestimonials", testimonials.DisplayTestimonials.Value, new { @class = "chkItems" })
            @Html.Hidden("Testimonials[" + i.ToString() + "].ResponseId", testimonials.ResponseId.ToString())
        </td>
        <td style="padding: 2px 0 2px 2px;">@Html.Label(testimonials.FirstName)</td>
        <td style="padding: 2px 0 2px 2px;">@Html.Label(testimonials.LastName)</td>
        <td style="padding: 2px 0 2px 2px;">@Html.Label("Testimonials[" + i.ToString() + "].Question5Answer", testimonials.Question5Answer.ToString())</td>
    </tr>
       i++;
    }
</table>
    <p>
    <input type="submit" id="Submit" value="Save" class="PremierSubmitButton" /></p>
</div>
}

1 个答案:

答案 0 :(得分:0)

不要依赖javascript,始终在服务器端验证数据。也许用户在浏览器中禁用了javascript。如果你需要ajax,请使用Ajax表单reqest。

保持干燥;)