2

Getting 500 Internal server error on post method in ajax call in MVC? What am I doing wrong in this? Please Check out once and help me out of this. It working perfectly, but suddenly gives me the error of this.

I've tried setting breakpoints at the Controller method. But it does not stop there. The alerts "clicked" and the next one work perfectly fine. But the controller method is not called. Any help is appreciated. Here are my ajax call and my controller method. here is my ajax call...

<script type="text/javascript">
    $('body').delegate(".btnUrl", "click", function (e) {
        debugger
        e.preventDefault();
        var url = $('.btnUrl').data('url');
        $('#loadingImage').show();
        $.ajax({
            url: url,
            type: 'POST',
            data: $('.form').serialize(),
            dataType: "json",
            success: function (data) {
                debugger
                if (data.Success) {
                    $('#loadingImage').hide();
                    loadPartial(data.Url);
                }
                else {
                    $('#loadingImage').hide();
                    loadPartial(data.Url);
                }
            }
        })
    })
</script>

My controller method

 public ActionResult AddCompany(CompanyViewModel model)
         {
            //check model validation
            if (ModelState.IsValid)
            {
                try
                {
                    //get login userid
                    var loginUserId = User.Identity.GetUserId<int>();

                    model.Countries = _countriesRepository.GetCountries();
                    if (_companyRepository.IsExist(model.Name ,model.Email))
                    {
                        TempData["ErrorMessage"] = "Company is already exist with Name and Email";
                        return Json(new { Success = false, Url = "/Company/AddCompany" });
                    }

                    var company = new Company()
                    {
                        Name = model.Name,
                        Country = model.Country,
                        Phone = model.Phone,
                        Email = model.Email,
                        ContactPerson = model.ContactPerson,
                        CreatedDate = DateTime.Now,
                        CreatedBy = loginUserId,
                        UserId = loginUserId
                    };

                    // add record to database
                    _companyRepository.AddorUpdate(company);
                    TempData["SuccessMessage"] = " Company added successfully.";
                    return Json(new {Success=true, Url = "/Company/Index" });
                }
                catch (Exception ex)
                {
                    TempData["ErrorMessage"] = " Something went wrong.Please try again";
                    return Json(new { Success = false ,Url = "/Company/AddCompany" });
                }

            }
            else
            {
                TempData["ErrorMessage"] = " Something went wrong.Please try again";
                return Json(new { Success = false , Url = "/Company/AddCompany" });

            }


        }
erdi yılmaz
  • 352
  • 1
  • 4
  • 15
Avleen
  • 65
  • 6
  • Did you try setting a breakpoint inside the catch clause? That should help you figure this out. – galdin Aug 31 '18 at 05:19
  • 1
    please share request detail from network tab of browser – Ankush Jain Aug 31 '18 at 05:22
  • could you please add your `ex` details in your question with inner exception so we'll find out exact problem – er-sho Aug 31 '18 at 05:36
  • This could be a CORS issue, check out this if your Client-side code tries to send HttpRequest to other URL, than your Page came from: https://stackoverflow.com/questions/50252175/http-status-code-500-net-core-cors-and-httpclient-errors – JohnIdlewood Aug 31 '18 at 05:49

0 Answers0