部分视图中的剃刀验证和ajax形式不起作用

时间:2013-08-20 23:49:58

标签: asp.net-mvc-4 razor asp.net-ajax

我有以下PartialView

@model Token.Creator.Site.Models.Customer
@{
    var cusModel = new Token.Creator.Site.Models.CustomerModel();
    ViewContext.FormContext = new FormContext();
    HtmlHelper.ClientValidationEnabled = true;
    HtmlHelper.UnobtrusiveJavaScriptEnabled = true;
}
<div class="modal fade" id="customereditmodal" tabindex="-1" role="dialog" aria-hidden="true">
    @using (Ajax.BeginForm("EditCustomer", "Customer", new AjaxOptions(), new {
        @class = "modal-dialog form-horizontal",
        id = "upload_s0pin"
    })) { 
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                @if (string.IsNullOrEmpty(Model.Companyname)) { 
                    <h4 class="modal-title">@Model.Gender.Type @Model.Title @Model.Firstname @Model.Lastname</h4>
                } else {
                    <h4 class="modal-title">@Model.Companyname</h4>
                }
            </div>
            <div class="modal-body">
                @Html.ValidationSummary()
                <div class="form-group">
                    @Html.LabelFor(cus => cus.Companyname, "Firmenname", new { @class = "col-lg-3 control-label" })
                    <div class="col-lg-9">
                        @Html.TextBoxFor(cus => cus.Companyname, new { @class = "form-control" })
                    </div>
                    @Html.ValidationMessageFor(cus => cus.Companyname);
                </div>
                <h4>Ansprechpartner</h4>
                <div class="form-group">
                    @Html.LabelFor(cus => cus.Gender, "Anrede*", new { @class = "col-lg-3 control-label" })
                    <div class="col-lg-9">
                        @Html.DropDownListFor(cus => cus.Gender, new SelectList(cusModel.Gender.ToList(), cusModel.Gender), new { @class = "form-control" })
                    </div>
                </div>
                <div class="form-group">
                    @Html.LabelFor(cus => cus.Title, "Titel", new { @class = "col-lg-3 control-label" })
                    <div class="col-lg-9">
                        @Html.TextBoxFor(cus => cus.Title, new { @class = "form-control" })
                    </div>
                    @Html.ValidationMessageFor(cus => cus.Title);
                </div>
                <div class="form-group">
                    @Html.LabelFor(cus => cus.Firstname, "Vorname*", new { @class = "col-lg-3 control-label" })
                    <div class="col-lg-9">
                        @Html.TextBoxFor(cus => cus.Firstname, new { @class = "form-control" })
                    </div>
                    @Html.ValidationMessageFor(cus => cus.Firstname);
                </div>
                <div class="form-group">
                    @Html.LabelFor(cus => cus.Lastname, "Nachname*", new { @class = "col-lg-3 control-label" })
                    <div class="col-lg-9">
                        @Html.TextBoxFor(cus => cus.Lastname, new { @class = "form-control" })
                    </div>
                    @Html.ValidationMessageFor(cus => cus.Lastname);
                </div>
                <div class="form-group">
                    @Html.LabelFor(cus => cus.Emailaddress, "Emailadresse*", new { @class = "col-lg-3 control-label" })
                    <div class="col-lg-9">
                        @Html.TextBoxFor(cus => cus.Emailaddress, new { @class = "form-control" })
                    </div>
                    @Html.ValidationMessageFor(cus => cus.Emailaddress);
                </div>
                <h4>Adresse</h4>
                <div class="form-group">
                    @Html.LabelFor(cus => cus.Street, "Straße und Hausnummer*", new { @class = "col-lg-3 control-label" })
                    <div class="col-lg-9">
                        @Html.TextBoxFor(cus => cus.Street, new { @class = "form-control" })
                    </div>
                    @Html.ValidationMessageFor(cus => cus.Street);
                </div>
                <div class="form-group">
                    @Html.LabelFor(cus => cus.ZIPCode, "Postleitzahl*", new { @class = "col-lg-3 control-label" })
                    <div class="col-lg-9">
                        @Html.TextBoxFor(cus => cus.ZIPCode, new { @class = "form-control" })
                    </div>
                    @Html.ValidationMessageFor(cus => cus.ZIPCode);
                </div>
                <div class="form-group">
                    @Html.LabelFor(cus => cus.Town, "Stadt*", new { @class = "col-lg-3 control-label" })
                    <div class="col-lg-9">
                        @Html.TextBoxFor(cus => cus.Town, new { @class = "form-control" })
                    </div>
                    @Html.ValidationMessageFor(cus => cus.Town);
                </div>
                <div class="form-group">
                    @Html.LabelFor(cus => cus.Country, "Land*", new { @class = "col-lg-3 control-label" })
                    <div class="col-lg-9">
                        @Html.TextBoxFor(cus => cus.Country, new { @class = "form-control" })
                    </div>
                    @Html.ValidationMessageFor(cus => cus.Country);
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Schließen</button>
                    <input type="submit" class="btn btn-primary" value="Speichern" />
                </div>
            </div>
        </div>
    }
</div>

模型类由EF自动生成。我将其扩展为应用验证属性。

[MetadataType(typeof(CustomerMetadata))]
public partial class Customer {
    private class CustomerMetadata {
        [Required(ErrorMessage = "Geben Sie einen Vornamen ein")]
        public object Firstname;
        [Required(ErrorMessage = "Geben Sie einen Nachnamen ein")]
        public object Lastname;
        [Required(ErrorMessage = "Geben Sie eine Straße ein")]
        public object Street;
        [Required(ErrorMessage = "Geben Sie eine Postleitzahl ein")]
        public object ZIPCode;
        [Required(ErrorMessage = "Geben Sie eine Stadt ein")]
        public object Town;
        [Required(ErrorMessage = "Geben Sie ein Land ein")]
        public object Country;
        [Required]
        [DataType(DataType.EmailAddress, ErrorMessage = "Bitte geben Sie eine gültige Emailadresse ein")]
        public object Emailaddress;
    }
}

但验证不起作用。我做错了什么?

编辑:部分视图由AJAX加载

2 个答案:

答案 0 :(得分:2)

如果您只想验证,请确保您的EditCustomer操作方法具有以下结构:

    [HttpPost]        
    public ActionResult Login(LoginModel model, string returnUrl)
    {
        if (ModelState.IsValid)
        {
            // It's ok! do what you need
            return RedirectToLocal(returnUrl);
        }

        // If we got this far, something failed, redisplay form            
        return View(model);
    }

如果您想要不显眼的验证 - 如果出现问题,表单将不会发布 - 您需要在web.config中启用它:

<appSettings>
    <add key="ClientValidationEnabled" value="true"/>
    <add key="UnobtrusiveJavaScriptEnabled" value="true"/>
</appSettings>

安装Jquery ValidationMicrosoft JQuery Unobtrusive Validation nuget包。

最后,将以下脚本添加到主布局(或通过bundle注册):

<script src="~/Scripts/jquery.validate.min.js")></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js")></script>

请注意,包含上述脚本的顺序很重要,如果以不同的顺序添加,不显眼的验证不起作用......

答案 1 :(得分:1)

我终于找到了解决方案,我只需要在局部视图中添加以下脚本:

<script type="text/javascript">
    $(document).ready(function () {
        $.validator.unobtrusive.parse("#customereditmodal > form");
    });
</script>

customereditmodal是div的id。

以下是解决方案http://forums.asp.net/post/4299121.aspx

的链接