Display Model State error after jQuery Ajax call in ASP.Net Core MVC

pandeygolu4200
 
on Oct 06, 2022 10:46 PM
1402 Views

Hi,

I have created one form where i am saving the data using form, but when submit form and my ModelState is getting invalid and still i am not getting error message. 

Please check and let me know what is wrong with my code.

public IActionResult Index(Student model)
{ 
    if (ModelState.IsValid)
    {
        ProviderPersonalInfoUpdate modelinfo = new ProviderPersonalInfoUpdate();
        modelinfo.UserId = model.ManageProvider.UserId;
        modelinfo.FirstName = model.ManageProvider.FirstName;
        modelinfo.MiddleName = model.ManageProvider.MiddleName;
        modelinfo.LastName = model.ManageProvider.LastName;
        modelinfo.DOB = dtdob;
        modelinfo.Email = model.ManageProvider.Email;
        modelinfo.StartDate = dtstartdate;
        modelinfo.TaxID = model.ManageProvider.TaxID;
        modelinfo.PTan = model.ManageProvider.PTan;
        modelinfo.NPI = model.ManageProvider.NPI;
        modelinfo.SSN = model.ManageProvider.SSN;
        modelinfo.Phone = model.ManageProvider.Phone;
        modelinfo.Fax = model.ManageProvider.Fax;
        modelinfo.AddressLine1 = model.ManageProvider.AddressLine1;
        modelinfo.AddressLine2 = model.ManageProvider.AddressLine2;
        modelinfo.StateID = model.ManageProvider.StateID;
        modelinfo.CityID = model.ManageProvider.CityID;
        modelinfo.Zipcode = model.ManageProvider.Zipcode;
        modelinfo.Website = model.ManageProvider.Website;
        modelinfo.UserId = HttpContext.Request.Cookies["UserId"] != null ? HttpContext.Request.Cookies["UserId"].ToString() : "";
        AccountApiCall accountApiCall = new AccountApiCall(Configuration["ABCAPI"]);
        model.ManageProvider.createdBy = HttpContext.Request.Cookies["FullName"] != null ? HttpContext.Request.Cookies["FullName"].ToString() : "";
        var Response = accountApiCall.PostAPIcall<GetProviderDetailModel>(modelinfo, "provider/personaldetailsupdate", HttpContext.Request.Cookies["Token"].ToString());            
        if (Response.status)
        {                 
            TempData["ProviderId"] = model.ManageProvider.UserId;
            TempData.Keep("ProviderId");
 
            return Json(Response);
        }
        else
        {
            return Json(Response);                  
        }
    }
    else
    {
        return View("Index");                        
    }
}

this is controller where i am getting ModelState invalid but still no error message is displaying

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Oct 14, 2022 07:11 AM

It is not possible to display error message after Ajax POST success to return to the view if ModelState is IsValid.

The only way is to send the request to the controller via Ajax and then render a partial view on your page.

With out partial view its not possible.