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