如何验证mvc弹出窗口

时间:2013-08-06 05:21:07

标签: asp.net-mvc-4 razor

我在弹出窗口中放置了以下代码。基本上我想要实现的是在这种情况下验证HRN所需的字段。遗憾的是,没有显示任何验证。请让我知道如何解决这个问题。谢谢

查看:

@model HHIMS_Web_App.Models.CModel

        <div>

        <fieldset id="AddNew">

               @using (Html.BeginForm("CreateList", "Details", FormMethod.Post, new { id = "frmAsset" }))
               {
                    @Html.AntiForgeryToken()
                    @Html.ValidationSummary(true)
                <div>
                    <div class="addConsumerInfo">
                        @Html.LabelFor(model => model.HRN)
                        @Html.EditorFor(model => model.HRN)
                        @Html.ValidationMessageFor(model => model.HRN)
                    </div>

                    <div class="consumerAddSmallBox">
                        @Html.LabelFor(model => model.DOB)

                        @Html.EditorFor(model => model.DOB)

                    </div>

                </div>


               }
            </fieldset>

        </div>


     <input type="button" id="btnSave" style="height:50px; width:85px; font-size:14px; background-color:#3399FF;white-space: normal" class="k-button" title="Save" value="Save"/>




<script type="text/javascript">


    $('#btnSave').click(function () {

           var dropdownlist = $("#Gender").data("kendoDropDownList").select();

          $.ajax({
                type: 'POST',
                url: "@Url.Action("CreateList","Details")",
                data: {

                    HRN: $("#HRN").val(),
                    DOB: $("#DOB").val()



                },

                 success: function () {
                   window.close()
                }
            });
        });

</script>

型号:

    [Required(ErrorMessage = "Required")]
    [DisplayName("HRN")]
    public string HRN { get; set; 

2 个答案:

答案 0 :(得分:0)

您可以尝试使用此表单验证

   $(document).ready(function () {

 $('#btnSave').click(function () {

           var validation = $("#frmAsset"); 
                       if (!validation.valid()) {
                         return false;
                        }
else
{

           var dropdownlist = $("#Gender").data("kendoDropDownList").select();

          $.ajax({
                type: 'POST',
                url: "@Url.Action("CreateList","Details")",
                data: {

                    HRN: $("#HRN").val(),
                    DOB: $("#DOB").val()



                },

                 success: function () {
                   window.close()
                }
            });
}
        });
});

答案 1 :(得分:0)

按钮必须在表单中。

试试这样,  

           @using (Html.BeginForm("CreateList", "Details", FormMethod.Post, new { id = "frmAsset" }))
           {
                @Html.AntiForgeryToken()
                @Html.ValidationSummary(true)
            <div>
                <div class="addConsumerInfo">
                    @Html.LabelFor(model => model.HRN)
                    @Html.EditorFor(model => model.HRN)
                    @Html.ValidationMessageFor(model => model.HRN)
                </div>

                <div class="consumerAddSmallBox">
                    @Html.LabelFor(model => model.DOB)

                    @Html.EditorFor(model => model.DOB)

                </div>

            </div>
<input type="button" id="btnSave" style="height:50px; width:85px; font-size:14px; background-       color:#3399FF;white-space: normal" class="k-button" title="Save" value="Save"/>

           }
        </fieldset>