I am not sure what I am missing, I have tried some other ways but I am getting the same view, Im stuck here from hours.
My problem is when I am trying to return a partial view or a Json form my controller to my jquery ajax function I get just my Json as view or just my partial. like this:
I am editing a record in a bootstrap modal. just like this example Edit Modal
my JS:
// Handle "Edit Person" button click.
// This will make an ajax call, get information for person,
// put it all in the modal and display it
$(document).on('click', '.edit-provider', function () {
var personId = $(this).attr('id');
$.ajax({
url: '/Providers/Edit',
type: 'GET',
data: { id: personId },
success: function (data) {
$('#mdleditprovider').html(data).modal('show');
}
});
});
// Handle submitting of new information for Person.
// This will attempt to save new info
// If save was successful, it will close the Modal and reload page to see updated info
// Otherwise it will only reload contents of the Modal
$(document).on('click', '#btn-submit-edit-provider', function () {
var self = $(this);
$.ajax({
url: '/Providers/Edit',
type: 'POST',
data: self.closest('form').serialize(),
success: function (data) {
if (data.success == true) {
$('#mdleditprovider').modal('hide');
} else {
$('#mdleditprovider').html(data);
}
}
});
});
</script>
my controller:
[HttpGet]
public ActionResult Edit(int? id)
{
if (id == null) return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
Provider provider = db.Providers.Find(id);
if (provider == null) return new HttpStatusCodeResult(HttpStatusCode.NotFound);
return PartialView("Modals/Providers/FrmEdit", provider);
}
[HttpPost]
public ActionResult Edit(Provider provider)
{
if (ModelState.IsValid)
{
db.Entry(db.Providers.Find(provider.Id)).CurrentValues.SetValues(provider);
db.SaveChanges();
return Json(new { success = true });
}
// else
return PartialView("Modals/Providers/FrmEdit", provider);
}
When I debug my js it never hits on succes in my second function. I am not sure if my error is not in this part of the code, but I think so.
Thanks
Update:
it hits on error im not sure why
Update (2)
So now im trying to found what is going on here, I noticed that my form has a default POST and I think maybe thats why my code does not hit on succes in my js, the defaut has to be called as my get action, In my js I am now trying to go to a difrent post action but it is not working is it possible to change those defaults? I am not sure if this will solve my problem I am just testing.
I explain my idea here:
in this controller I want to fill a modal with Edit action result, and then get my post in my Update action
[HttpGet]
public ActionResult Edit(int? id)
{
if (id == null) return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
Provider provider = db.Providers.Find(id);
if (provider == null) return new HttpStatusCodeResult(HttpStatusCode.NotFound);
return PartialView("Modals/Providers/FrmEdit", provider);
}
[HttpPost]
public ActionResult Update(Provider provider)
{
if (ModelState.IsValid)
{
db.Entry(db.Providers.Find(provider.Id)).CurrentValues.SetValues(provider);
db.SaveChanges();
return View("Index",db.Providers.ToList());
}
// else
return PartialView("Modals/Providers/FrmEdit",provider);
}
this is the js I am using, but I get a http notfound (Edit post)
<script>
// Handle "Edit Person" button click.
// This will make an ajax call, get information for person,
// put it all in the modal and display it
$(document).on('click', '.edit-provider', function () {
var personId = $(this).attr('id');
$.ajax({
url: '/Providers/Edit',
type: 'GET',
data: { id: personId },
success: function (data) {
$('#mdleditprovider').html(data).modal('show');
}
});
});
$(function () {
$('#frmeditprovider').on('submit', function (e) {
e.preventDefault();
$.ajax({
url: '/Providers/Update', //this is the submit URL
type: 'POST', //or POST
data: $('#subscribe-email-form').serialize(),
success: function (data) {
alert('successfully submitted');
}
});
});
});
</script>