Overriding routevalues for childaction - asp.net-mvc-3

I have an index view that takes the page parameter like so:
/News?page=2
But in the layout for that view I have this childaction:
#{Html.RenderAction("Index", "Comments", new {page=1, pagesize = 10});}
But the querystring "page" remains 2.. how come? And how to override page for the childaction?

That's because the child action first looks at the original request query string when binding for values and after that the one passed as argument to the RenderAction helper. You could use a different name for this parameter to avoid this ambiguity.
UPDATE:
Unable to reproduce the behavior you are describing.
Controller:
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
[ChildActionOnly]
public ActionResult Test(string page)
{
return Content(page, "text/html");
}
}
View (~/Views/Home/Index.cshtml):
#{Html.RenderAction("Test", "Home", new { page = 1 });}
When querying /Home/Index?page=5 the correct value 1 is shown and the page parameter in the child action is 1.
Obviously if inside your child action you are fetching this value manually from the request it won't work but that's not something you should be doing anyways:
[ChildActionOnly]
public ActionResult Test()
{
string page = Request["page"] as string;
return Content(page, "text/html");
}

Related

Returning to current view after actions in _Layout.cshtml

I'm trying to wrap my head around MVC.NET 3.
I use a _Layout.cshtml as base (structure, navigation). As part of the layout I want to display two links used for changing language/localization.
These should be displayed and clickable no matter what page is viewed, and after changing the localization I want to reload the view that called the action. So the page that the customer is looking at will be reloaded, with new localization set.
One way is to copy and paste the localization-changing action in each of the sites controllers, but is there no easier and more elegant way?
I tried creating a specific controller that handles the localization changing, but can't figure out how to return the viewer to the previous controller.
Perhaps this is easier accomplished with jquery?
This is the DIV from the _Layout file with the language changing buttons. It calls the action in the current controller, which means I have to define it in each of the site's controllers. (The good thing is the view that is returned is always correct.)
<div id="top>
#using (Html.BeginForm())
{
<button id="sv_flag" name="localization" title="#Resources.Global.Sv_Flag_Hover" value="sv-SE" />
<button id="en_flag" name="localization" title="#Resources.Global.En_Flag_Hover" value="en-GB" />
}
</div>
I also tried using a specific controller for this, but cannot think of how I could return to the current view afterwards? Like so:
#using (Html.BeginForm("LocalizationAction", "LocalizationController"))
...
Edit
Now using the suggestion from Darin, I send in the controller and action values from the layout page:
#using (Html.BeginForm("SetLocalization", "Localization",
new { returnController = #ViewContext.Controller.ValueProvider.GetValue("controller").RawValue,
returnAction = #ViewContext.Controller.ValueProvider.GetValue("action").RawValue }))
...
But I cannot get the localization changes to work, my controller action looks like this:
public ActionResult SetLocalization(string localization, string returnController, string returnAction)
{
Thread.CurrentThread.CurrentCulture = Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(localization);
return RedirectToAction(returnAction, returnController);
}
You could pass a returnUrl:
#using (Html.BeginForm("LocalizationAction", "LocalizationController", new { returnUrl = Request.Url.AbsoluteUri }))
{
...
}
and inside your LocalizationAction redirect to this url:
public ActionResult LocalizationAction(string returnUrl)
{
... do your localization stuff and once you are done get back:
return Redirect(returnUrl);
}
obviously you could do a little checking before blindly redirecting. Things like whether the returnUrl parameter is not empty and whether it belongs to your domain. You may take a look at how the default AccountController does that once it authenticates a user.
return Redirect(Request.UrlReferrer.ToString());
public ActionResult Change(String LanguageAbbrevation)
{
if (LanguageAbbrevation != null)
{
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(LanguageAbbrevation);
Thread.CurrentThread.CurrentUICulture = new CultureInfo(LanguageAbbrevation);
}
HttpCookie cookie = new HttpCookie("Language");
cookie.Value = LanguageAbbrevation;
Response.Cookies.Add(cookie);
return Redirect(Request.UrlReferrer.ToString());
}
I found a completely different (and much easier and elegant) solution to my problem.
I simply created a BaseController, that holds the action for changing the localization.
Then all controllers I add to the site inherit from this BaseController. This gives a single location for the code and does not require sending any return parameters, etc.
BaseController:
public class BaseController : Controller
{
[HttpPost]
public ActionResult Index(string localization)
{
Thread.CurrentThread.CurrentCulture = Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(localization);
return View();
}
}
Each of the site's Controllers then only need to inherit from it, and then mind their own actions:
public class ApplicationsController : BaseController
{
//
// GET: /Applications/
public ActionResult Index()
{
return View();
}
}
...

why TempData[] doesnt work with IE

İn my MVC3 project, there is plenty of TempData[] that I am using for passing datas between actions. And it works totaly perfect when I use Chrome. But in IE I can't get values of TempData[] items. if anyone knows whats the problem and how can I solve it?`
public class SomeController : Controller
{
public ActionResult SomeAction()
{
TempData["id"] = "someData";
return View();
}
}
public class AnotherController : Controller
{
public ActionResult AnotherAction()
{
string data = Convert.ToString(TempData["id"]);
return View();
}
}
`
You should never return a view from a controller action that stores something into TempData. You should immediately redirect to the controller action that is supposed to use it:
public class SomeController : Controller
{
public ActionResult SomeAction()
{
TempData["id"] = "someData";
return Redirect("AnotherAction", "Another");
}
}
public class AnotherController : Controller
{
public ActionResult AnotherAction()
{
string data = Convert.ToString(TempData["id"]);
return View();
}
}
The reason for this is that TempData survives only for a single additional request. So for example if inside the view you are sending an AJAX request to some controller action (no matter which) and then have a link in this view pointing to the target action, when the user is redirected to this target action TempData will no longer exist since it was lost during the AJAX request done previously.
If you need to store data for longer than a single redirect you could use Session.
If you need to store data for longer than a single redirect you should use Keep or Peek methods.
string data = TempData["id"].;
TempData.Keep("id");
or simply use,
string data = TempData.Peek("id").ToString();
Peek function helps to read as well as advice MVC to maintain “TempData” for the subsequent request.

Add route for asp.net mvc 3

I need to be able to hande routes like this:
appdomain/city/City-state, so in case somebody used
appdomain/Washington/Washington-DC he retrieves proper info from proper controller action. For now can`t get what controller and action it should be to get this url and handle it properly.
To clear it a bit, there`s like no controller and action, but 2 parameters instead of them.
Why not adding a little help from a fixed path, like Show-City
routes.MapRoute(
"CityAndState",
"Show-City/{city}/{state}",
new { controller = "Cities", action = "Index", id = UrlParameter.Optional }
);
this will never interfere with your existing routes, and then you can use:
http://domain.com/Show-City/New York/NY
at your Index Action inside the Cities Controller you would have something like:~
public class CitiesController : Controller
{
public ActionResult Index(string city, string state)
{
// use city and state variables here
return View();
}
}
Try this:
routes.MapRoute("Foo", "{state}/{city}",
new { controller = "ControllerName", action = "ActionName" });
and in your class you'd have:
public class ControllerNameController : Controller {
public ActionResult ActionName(string state, string city) {
...
}
}

Clean Url for MVC index method with parameter

I'm new to MVC and Google hasn't been much help so I will ask here.
What I'm trying to do is simple (I would had thought) I want to pass a string to the index method but it generally looks like:
http://mydomain.com/home/index/mystring
and I want:
http://mydomain.com/mystring
How do I go about that?
You could define the following route in Global.asax:
routes.MapRoute(
"MyStringRoute",
"{*mystring}",
new { controller = "Home", action = "Index" }
);
which will invoke the Index action of the Home controller:
public class HomeController : Controller
{
public ActionResult Index(string mystring)
{
return View();
}
}

gets called from separate controller (ASP.NET MVC3)

I'm trying to call an action in a separate controller with an actionlink.
The problem is that both the [HttpGet] and the [HttpPost] action gets called, and since the post-method returns the view from which the action is called, nothing gets displayed.
Get method:
[HttpGet]
public ActionResult View(int id, int index)
{
var form = formService.GetForm(id);
var pageModel = new PageViewModel();
var page = form.Pages.ElementAt(index);
ModelCopier.CopyModel(page, pageModel);
ModelCopier.CopyModel(form, pageModel);
return View(pageModel);
}
Post Method
[HttpPost]
public ActionResult View(PageViewModel pageViewModel)
{
if (!ModelState.IsValid)
{
return RedirectToAction("Details", "Forms", new { id = pageViewModel.FormId });
}
var pageToEdit = pageService.GetPage(pageViewModel.PageId);
ModelCopier.CopyModel(pageViewModel, pageToEdit);
pageService.SavePage();
return RedirectToAction("Details", "Forms", new {id = pageViewModel.FormId});
}
How it's called from the View (from a view returned by another controller):
#Html.ActionLink("View", "View", "Pages", new { id = Model.FormId, index = item.Index-1 }, null)
What am I doing wrong here? I essentially want it to work as an update/edit function. And the view returned contains a simple form for the viewmodel.
An action link issues a GET request. You will have to implement some JavaScript function that captures the url and parameters, and dynamically creates and submits a new form with a POST method, or does an Ajax POST. You could write your own HTML Helper, to wrap this functionality, but the default functionality of clicking an <a> tag (which is what is generated by a Html.ActionLink) will issue a GET request.
I don't think you'll be able to use an action link - see this question
The options are to use jQuery to post data or swap to a simple form and submit

Resources