简体   繁体   English

当部分视图在另一个视图中时,Mvc 模型验证错误不显示

[英]Mvc Model Validation Errors Not Showing when partial view Inside The another view

When i try to load partial view directly in browser and submit it displaying the error messages but if that same partial view is in another view it is not showing that model error validations.当我尝试直接在浏览器中加载局部视图并提交它时显示错误消息,但如果同一个局部视图在另一个视图中,则不会显示该模型错误验证。 to load partial view inside the view i'm using Ajax.Beginform Method.在我使用 Ajax.Beginform 方法的视图中加载局部视图。

When Loading partial view in browser在浏览器中加载局部视图时在此处输入图片说明

When including inside the another view当包含在另一个视图中时在此处输入图片说明

My Partial View我的部分观点

@model FPW.FPWClientModels.SiteClientModel
@if (this.ViewContext.FormContext == null)
{
    this.ViewContext.FormContext = new FormContext();
}

    @using (Ajax.BeginForm("CreateSite", "Site", null, new AjaxOptions
    {
        HttpMethod = "POST",
        AllowCache = false,
        ////LoadingElementId = "AjaxOverlay",
        //OnSuccess = "SiteOnSaveSuccess",
        //OnFailure = "SiteOnSaveFailure",
    }, new { @id = "SiteCreateForm" }))
    {
        <div class="modal-body">
            @Html.AntiForgeryToken()            

            <div class="form-group">
                @Html.LabelFor(model => model.SiteName, htmlAttributes: new { @class = "control-label" })
                <div class="col-md-12">
                    @Html.EditorFor(model => model.SiteName, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.SiteName, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.SiteAddress, htmlAttributes: new { @class = "control-label" })
                <div class="col-md-12">
                    @Html.EditorFor(model => model.SiteAddress, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.SiteAddress, "", new { @class = "text-danger" })
                </div>
            </div>


        <div class="modal-footer">
            <div class="form-group">
                <div class="col-md-offset-2 col-md-10">
                    <input type="submit" value="Create" class="btn btn-default" />
                </div>
            </div>
        </div>

    }

    <script>
        function SiteOnSaveSuccess(resp) {            
        }

        function SiteOnSaveFailure(resp) {            
        }
    </script>

My Controller我的控制器

public ActionResult CreateSite()
    {
        SiteClientModel oSiteClientModel = new SiteClientModel();
        return PartialView(oSiteClientModel);
    }

    //Create Site
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult CreateSite(SiteClientModel oSiteClientModel)
    {
        if (ModelState.IsValid)
        {
            var saveSiteDetails = oSiteApiController.CreateSiteDetails(oSiteClientModel);
            return PartialView(saveSiteDetails);
        }
        else
        {
            oSiteClientModel.ReturnValue = new ReturnValue { IsSuccess = false, ReturnType = ReturnType.Error, Message = "Form Not Valid" };
        }
        return PartialView("CreateSite",oSiteClientModel);
    }

Found the error.发现错误。 It was not used Update target id in ajax option.它没有在 ajax 选项中使用更新目标 ID。 After use UpdateTargetId and pointed it to the form id it is working fine as expected使用 UpdateTargetId 并将其指向表单 ID 后,它按预期工作正常

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

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