0

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:

enter image description here

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>
Community
  • 1
  • 1
bto.rdz
  • 6,636
  • 4
  • 35
  • 52

3 Answers3

0

It looks like everything is working as expected given your code example.

The following code is getting executed (judging from the screeshot):

if (ModelState.IsValid)
{
    db.Entry(db.Providers.Find(provider.Id)).CurrentValues.SetValues(provider);
    db.SaveChanges();
    return Json(new { success = true });
}

Specifically, return Json(new { success = true }); is rendering the response you're seeing in your browser.

jrmyward
  • 136
  • 1
  • 4
  • The problem is that I dont want to see that, I want that Json as my data parameter in on succes jquery ajax call – bto.rdz Jan 04 '14 at 00:37
  • What is the error you're seeing in the javaScript console? Do you have an example app to help debug? – jrmyward Jan 04 '14 at 06:47
0

Try adding "datatype" parameter to ajax request:

$.ajax({

  dataType: "json",

  url: url,

  data: data,

  success: success

});

http://api.jquery.com/jQuery.getJSON/

vmg
  • 9,920
  • 13
  • 61
  • 90
  • Not working, I cant even degub my js, because it does not stop in on error, I added and alert on error and it shows it really fast that's how I know it is hitting error. – bto.rdz Jan 04 '14 at 00:55
0

For your url location try using the '@Url.Action("Edit","Providers")'.

sherebry
  • 184
  • 1
  • 3