-2

i'm new with ajax and i'm trying to call a post action from an ajax method like that

 $(".buttonSelection").click(function () {

    selectedId = $(this).parents('tr:first').children('td:first').children('input:first').attr('value');
    $.ajax({
        // Call MaSelection action method
        url: "/DemandeLocation/MaSelectionOffre",
        data: { id: selectedId },
        type: 'Post',
        success: function (msg) {
            window.location.replace('@Url.Content("~/DemandeLocation/MaSelectionOffre")');
        },
        error: function (xhr) {
            alert("something seems wrong");
        }
    });
    });

my post method goes with success but instead of redirectin me to the MaSelection View it return the first view where i call the method, so i tried to put a "Success" fragment in my ajax method and i puted a location replace by "Ma selection" view but i know that the view lose the id so it become null, how can i do it with Ajax,

here my post action for more details

[HttpPost]
    [Authorize(Roles = "Locataire")]
    public ActionResult MaSelectionOffre(string id)

    {
        int DemandeLocationGetbyId = Convert.ToInt32(id);

        var selectionOffre = db.SelectionOffreLocationSet.Where(model => model.DemandeLocationPublication_ID == DemandeLocationGetbyId).ToList();

        return View("MaSelectionOffre", selectionOffre);
    }
404NotFound
  • 635
  • 1
  • 6
  • 14

3 Answers3

1

use json as datatype;

 $(".buttonSelection").click(function () {

    selectedId = $(this).parents('tr:first').children('td:first').children('input:first').attr('value');
    $.ajax({
        // Call MaSelection action method
        url: "/DemandeLocation/MaSelectionOffre",
        dataType:"json",
        data: { id: selectedId },
        type: 'Post',
        success: function (msg) {
            window.location.href = msg.redirect;
        },
        error: function (xhr) {
            alert("something seems wrong");
        }
    });
    });

also you need this ;

Convert object to JSON string in C#

Community
  • 1
  • 1
  • it's not a problem with data type, my action get the id and return the list of items, i tried this but it gave me something seems wrong – 404NotFound Jun 17 '14 at 11:26
1

If you want redirect page, after ajax call you should use

...
success: function (msg) {
    window.location.href = '@Url.Action("MaSelectionOffre", "DemandeLocation")';
},
...

EDIT

If you want replace result, use something like following:

HTML

<div id="updateTargetId">
    //table
        //tr
            //td
                //your button that has cssClass buttonSelection
</div>

JS

$(".buttonSelection").click(function () {

    selectedId = $(this).parents('tr:first').children('td:first').children('input:first').attr('value');
    $.ajax({
        // Call MaSelection action method
        url: "/DemandeLocation/MaSelectionOffre",
        dataType:"json",
        data: { id: selectedId },
        type: 'Post',
        success: function (msg) {
            $("#updateTargetId").html(msg);
        },
        error: function (xhr) {
            alert("something seems wrong");
        }
    });
});

CONTROLLER (return PartialView)

[HttpPost]
[Authorize(Roles = "Locataire")]
public ActionResult MaSelectionOffre(string id)

{
    int DemandeLocationGetbyId = Convert.ToInt32(id);

    var selectionOffre = db.SelectionOffreLocationSet.Where(model => model.DemandeLocationPublication_ID == DemandeLocationGetbyId).ToList();

    return PartialView("MaSelectionOffre", selectionOffre);
}
AliRıza Adıyahşi
  • 15,658
  • 24
  • 115
  • 197
  • here's the scenario, i call the MaselectionAction that return a list of item to the view MaSelection, the problem that when i put the retrun view with selectionOffre list it works as well as i want, but after the sucess of the action , it returns to the ajax and check sucess fragment, when i put window.location.href = '@Url.Action("MaSelectionOffre", "DemandeLocation")'; the view lost Selected list and return "The resource cannot be found". – 404NotFound Jun 17 '14 at 11:36
0

i changed my action to a get action and in my button i just added window.location.replace with link and ID

<button type="button" class="buttonSelection" onclick="window.location.replace('@Url.Content("~/DemandeLocation/MaSelectionOffre?id="+item.Publication_ID)')"> <span class="ui-icon ui-icon-cart"></span> </button>
404NotFound
  • 635
  • 1
  • 6
  • 14