Search code examples
c#asp.net-mvcmodelasp.net-mvc-5addmodelerror

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.

When Loading partial view in browser enter image description here

When including inside the another view enter image description here

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);
    }

Solution

  • Found the error. It was not used Update target id in ajax option. After use UpdateTargetId and pointed it to the form id it is working fine as expected