在mvc中的ajax调用中获取500内部服务器错误

时间:2018-08-31 05:14:43

标签: c# asp.net-mvc

在MVC中的ajax调用中,post方法获取500内部服务器错误? 我在这方面做错了什么?请结帐一次,并帮我解决这个问题。 它工作正常,但是突然给了我这个错误。

我尝试在Controller方法中设置断点。但这并不止于此。警报“已单击”,下一个警报运行良好。但是未调用controller方法。任何帮助表示赞赏。这是我的ajax调用和控制器方法。 这是我的ajax电话...

<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>

我的控制器方法

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

            }


        }

0 个答案:

没有答案