Rune Hansen
Rune Hansen

Reputation: 1024

How to make an action method render a particular named view?

I have this action method SearchAroundMe(), and then one thinks that it will render the view "NoViewHasThisName", but to my surprise the method insist on rendering the default view "SearchAroundMe", How to make the action method render the "NoViewHasThisName" view?

I have uploaded a simple test application to Skydrive with this problem: https://skydrive.live.com/?cid=5c0bc0a6f7bdc3c6#cid=5C0BC0A6F7BDC3C6&id=5C0BC0A6F7BDC3C6%21164

If you just run the application you will see the problem. Because I am using a location aware browser, the app will ask allowance to track your physical position, don't worry about this, as I am not collecting any information or doing anything bad.

**[HttpPost] 
public ActionResult SearchAroundMe(double latitude, double longitude) 
{ 
    return View("NoViewHasThisName"); 
}**

Upvotes: 1

Views: 196

Answers (4)

Rune Hansen
Rune Hansen

Reputation: 1024

Thank you to Brandon, your observations yesterday was very important in tracking down the problem. I found the solution to the problem. Using jQuery.post() caused some problems, but when I switched to transport the data from the view to the controller using form submit, suddenly there was no problens anymore :-)

@using (Html.BeginForm("SearchAroundMe","News")) 
{        
        @Html.Hidden("latitude");
        @Html.Hidden("longitude");
}

var latField = $("#latitude");
var longField = $("#longitude");

latField.val(latitude);
longField.val(longitude);

$('form').submit();

Upvotes: 0

Toc
Toc

Reputation: 163

You can try this:

return RedirectToAction("Action", "Controller");

And create an empty action :

public ActionResult NoViewHasThisName()
{
    return View();
}

Upvotes: 0

Brandon
Brandon

Reputation: 69993

You're doing an ajax post, but you're not actually doing anything with the result.

You're not leaving the page at all. Take a look at Firebug after you allow your location, it performs the post and it returns the view "NoViewHasThisName", but you don't see it because you haven't defined what to do with the result.

Replace your .post call with this and you'll see what I mean

    $.ajax("News/SearchAroundMe", {
      type: "POST",
      async: false,
      data: { latitude: latitude, longitude: longitude },
      success: function (response) {
          $("body").html(response);
      }
    });

Obviously this isn't exactly what you want to do, but it's just an example.

Upvotes: 1

amurra
amurra

Reputation: 15401

Just put the path to the NoViewHasThisName.aspx view you want to return:

[HttpPost] 
public ActionResult SearchAroundMe(double latitude, double longitude) 
{ 
    return View("~/Views/NoViewHasThisName.aspx"); 
}

Upvotes: 0

Related Questions