JayL
JayL

Reputation: 101

MVC controller's method not returning view

I tried many ways and it's still not working... I'm calling controller's method with ajax call. Thanks to answer to my previous question it's working fine, I have data that I wanted to send from view in controller (in CreateIncident method). Problem is that controller should render new view and redirect to it, but it's not happening. For now I just want to see new view, nothing else, I'll deal with recieved data later. Any idea why is this happening? Is this because I'm calling method with ajax and not by e.g. simple Url.AcionLink?

Ajax call to method:

function addMarker(location, fulladdress) {

        var data = JSON.stringify(fulladdress) + JSON.stringify(location)

        $.ajax({
            type: "POST",
            url: "Incidents/CreateIncident",
            dataType: "text",
            data:  {JsonStr : data} 
        })
    }

Controller:

    public ActionResult Create()
    {
        Incident newIncident = new Incident();
        newIncident.AddDate = DateTime.Today.Date;
        newIncident.DateOfIncident = DateTime.Today.Date;
        newIncident.TimeOfIncident = DateTime.Today.TimeOfDay;

        return this.View(newIncident);
    }

    [HttpPost]
    public ActionResult CreateIncident(string JsonStr)
    {

   //   RedirectToAction("Create"); //none of this three is working
   //   return View("Create");
        return Redirect("Create");
    }

No matter if I'm trying to access CreateIncident or Create the method is called, but there's no redirect to /Incidents/Create (I'm calling from Home/Index). Any ideas why? I would like to redirect to Create.cshtml straight from CreateIncident so I wouldn't have to pass data between methods, but any solution will do fine.

Upvotes: 3

Views: 15170

Answers (4)

Moe
Moe

Reputation: 1599

The redirect in that case has to be done through the AJAX call. Call your action method and do your logic, then redirect on success.

$.ajax({
 type: "POST",
 url: "Incidents/CreateIncident",
 dataType: "text",
 data:  {JsonStr : data} ,
 success: function (data) {
 window.location.href = "Incidents/Create";
 }
})

Upvotes: 5

Ravi Rupeliya
Ravi Rupeliya

Reputation: 621

Yes redirecting page in success event of ajax call is the only solution, as you are making ajax call not submitting a form. Browser redirects automatically after post only if you are posting a form.

However you can use following code if you don't want to redirect it in success event.

function postData(url, allData){
    form = document.createElement('form');
    for(data in allData)
    {
        var input = document.createElement('input');
        input.type = 'text';
        input.name = data;
        input.value = allData[data].toString();
        form.appendChild(input);
    }
    form.method = 'post';
    form.action = url;
    form.submit();
}

and use this function like this :

function addMarker(location, fulladdress) {
    var data = JSON.stringify(fulladdress) + JSON.stringify(location);
    postData('/Incidents/CreateIncident', {JsonStr : data})
}

warning : haven't tested :).

Upvotes: 0

iuliu.net
iuliu.net

Reputation: 7145

Use the view's full path instead of its name.

return View("/Incidents/Create");

Upvotes: 0

Duha
Duha

Reputation: 829

try:

url:"../Incidents/CreateIncident"

put in $ajax call error handling and see the error, it will help you

$.ajax({
    type: "POST",
            url: "Incidents/CreateIncident",
            dataType: "text",
            data:  {JsonStr : data},
   success: function(result){
        // Do stuff
   },
 error: function(xhr){
        alert('Request Status: ' + xhr.status + ' Status Text: ' + xhr.statusText + ' ' + xhr.responseText);
    }
 });

Upvotes: 1

Related Questions