MVC 3 Routing to an Area - asp.net-mvc-3

How does one Route from one Area to another?
In the default project, there are no area's, but once you add an Area, how could I redirect from the default Home page to a page inside my new Area?
I don't see a RedirectTo* method which takes a parameter for an Area name anywhere.
Unless I'm missing the point of Area's completely?

Inside our views, we don’t need to specify the area route data value when generating
links to other controller actions inside that area. We only supply the action name, because the controller and area name will come from the existing route data for the current request. If we want to link to an outside area, we’ll need to supply that route data explicitly.
return RedirectToAction("yourAction", "YourController", new { area = "yourArea" });
The "area" route value needs to match the AreaName used in the AreaRegistration class for the URL to generate correctly.

This code in your view will link you to the Admin area, regardless of which area you are currently in:
#Html.ActionLink("Click Me", "ActionName","ControllerName",new { Area = "AreaName"}, null )
e.g. (poor made up example)
#Html.ActionLink("Administer User", "Home","UserAdmin",new { Area = "Admin"}, null )
The final null in the call corresponds to HtmlAttributes, and I usually leave it as null.

Related

prefill or pass query parameters to child resource

How I can get access to query parameters in child resource create form?
For example if I want to have a button on parent resource edit page which redirects to
/childresource/create?parentid=123 and then on child resource create form we have <SelectInput source="parentid" /> and I want this to be preselected?
Is it somehow possible already? Would it be better with custom react routing for example to /parent/123/addchild and have whole custom create component or maybe just customer selectinput component?
I already have functional create page for child but it would be really nice to somehow prefill values.
Answer to your second question:
ProductSelect.defaultProps = {
location: PropTypes.object.isRequired,
};
// This is the component exported and known by admin-on-rest, not ProductSelect
const EnhancedProductSelect = withRouter(ProductSelect);
// Hence, this is the one which needs a defaultProp for addField
EnhancedProductSelect.defaultProps = {
addField: true
};
export default EnhancedProductSelect;
Does this help for the first question ?
https://codesandbox.io/s/pp0o4x40p0
The relevant code parts are:
the CreateCommentButton component inside src/posts.js
the CommentCreate component inside src/comments.js (note how we set the defaultValue prop on SimpleForm)

How do I get the Area (or the path) of where a View is being scaffolded?

PROBLEM:
I am customizing the MVC 5 templates for scaffolding views.
I need to be able to get the area name for the area where the view is being created.
EG, I want to get be able to set the value for the variable areaName for the Url Action params:
Url.Action("Index", "<#= ViewDataTypeShortName#>s", new { area = "<#= areaName #>", page<#= ViewDataTypeShortName#>s = x }), ViewContext, NormalizePath("~/Areas/<#= areaName #>/Views/<#= ViewDataTypeShortName#>s/"))
In a controller, I can do:
<#
var areaName = GeneratedTextTransformation.AreaName;
#>
But in the View T4 template this is not available.
So how would I get the name of the area where I am creating the view?
EDIT:
Because MVC uses convention over configuration, I could get the area if I had a way to get the path of the View file that is being created (as in:
/Areas/[Area Name]/Views/
So the question reduces to:
QUESTION:
How do I get the path of where a View is being created by the T4 ASP.NET MVC scaffolding?
I have solved this by using the AreaName property (available in T4 template for controller) in ViewBag.AreaName and reading this in the view.
NOT pretty, but it will work.
However, I would much sooner know how to access the Area the files are being created in in my T4 code. See the edit to my question.

kohana view from what controller?

I have a view that could be called from any of 3 actions from one controller.
But, that view should be slightly different depending on what action caused it (it should display 3 icons or 2 or one depending on action called). Can I check in the view what action caused it so I can use if statement to check whether display each icon or not?
Thank you.
Of course, you can pass action value directly to the view:
$this->template->action = Request::current()->action();
But View should not know anything about Request properties, its a Controller logic. I suggest you to pass special flags from your actions:
public function action_show1()
{
// show only one icon
$this->template->icons = array('first');
}
public function action_show2()
{
// show another two icons
$this->template->icons = array('second', 'third');
}
public function action_showall()
{
// show all icons
$this->template->icons = array('first', 'second', 'third');
}
Or set special flag (variable) for every icon.

How to change WebGrid action for getting data (.NET MVC3)

I have a Partial View that renders WebGrid. My controller looks like
public ActionResult Index()
{
return View();
}
public ActionResult GetUserList(int? page, string sort, string sortdir)
{
var model = UserModel.getList(page,sort,sortdir);
return PartialView("_UserList",model);
}
Index.cshtml :
....
#Html.Action("GetUserList")
The problem is that every time I click on grid navigation or sort links it calls Index method. How can I make Webgrid to execute a different action (GetUserList in this case)? I'm sure I can prepend GetUserList to all links in grid using jquery, but I believe it should be a better way.
It's also possible that what I'm doing is completely wrong, so thanks for your suggestions.
After lot of monkeying around and digging (and even fiddling with Reflector with WebGrid's source code), I came to the conclusion that with WebGrid, you cannot control/change the Header link action.
To create the header link URL, the path is taken from HttpContext.Request.Path, so there is no way to customize it to point to a different route.
One very ugly hack would be to tap into to jQuery Ajax's events (since the header link uses jQuery.load to sort) and overwrite the URL:
Album Id
Better solution would be to use:
Telerik Grid which lets you specify custom routes and also offers much more flexibility in rendering your layout
or MvcContrib Grid (not sure if this lets you modify header links but definitely offers more flexibility than WebGrid)
#MrChief had the idea above about the ugly hack...I put that together. Here is the main code that I used to do this. It does, indeed, hijack the ajax call before it is put on the wire. The key is to modify the URL that is getting sent because the grid will grab that URL from HttpContext.Request.Path. and plug it into the onclick for the anchor element.
I put this into my main common.js and will simply attach a function to capture the ajaxSend event which happens just before the data is sent.
// Used to hijack the sending of all AJAX calls. Before it sends the call to the server, it checks to see if the
// active element (the element that prompted the call) is marked with a given class. If so, then it will perform
// the given operation.
$(document).ajaxSend(function (event, jqXHR, ajaxOptions) {
var activeElement = document.activeElement;
if ($(activeElement).attr('redosorturl') != null) {
// If this is a sort anchor link from a grid that needs to have the sort link redone, do it here.
// the code is in the eipGrip.js file.
if ($(activeElement).attr('redosorturl').toString() == 'redoSortURL') {
var newURL = RedoGridSortURL(activeElement, ajaxOptions.url.toString());
ajaxOptions.url = newURL.toString();
}
}
return false;
});
When rendering the page, I have marked the tag in column header that contains the incorrect URL with a class named "redosorturl', so I know when I hijack the ajax call, the operation has to be done on this element. I then call a custom function that gives me the correct URL, then the ajaxOptions.url is then rewritten with that new URL.
I have to pass the activeElement to that rewrite function so I can traverse up the DOM to get the grid information, where I have put data like the controller and action method that is used along with and IDs and other info that I use for the URL. Likewise, I pass in the current url string because the grid will inject a token at the end of the url that I parse off and put on the new url.
Your conclusion isn't right. You just need to wrap your webgrid in a Get form:
using (Html.BeginForm("GetUserList", "ThingaMaBob", System.Web.Mvc.FormMethod.Get))
{
var grid = new WebGrid(
...
));
Html.Hidden(grid.SortFieldName, grid.SortColumn);
Html.Hidden(grid.SortDirectionFieldName, grid.SortDirection == SortDirection.Ascending ? "ASC" : "DESC");
}
The hiddens are so that the sort dir and sort field end up in parseable form in the querystring. You end up with urls like localhost/ThingaMaBob/GetUserList?someotherfields=whatever=&sort=city&sortdir=ASC
If you remove [HttpPost] attribute and let the route come to the same function. you'll find the Request["page"] value in your method. this will allow you to put a check on Request["Page"] value.

How do you handle displaying navigation and sub navigation in an MVC app?

I'm having trouble determining where to place navigation for an MVC app. For example, say you have the following structure:
Conferences
South Eastern Conference
Florida Gators
Georgia Bulldogs
Arkansas Razorbacks
Pac-10
USC
Hawaii
Big East etc...
How would you best create a structure for implementing a 'main' navigation and subsequent 'sub' navigation? Using the hypothetical example, You'd have specific sub navigation for each conference, showing its respective colleges (and only that conferences colleges).
Is this something you'd handle in the main view and just hide the non-selected conference?
Or would you create a menu helper (or yet another partial) and call that from each individual college's view?
Best way is to use multiple, nested master pages. e.g. Site.master would contain your top-level nav (list of conferences?) then you'd have a different master page for each conference that would 'extend' site.master. You can, in theory, have as many nested master pages as you want. Finally, Florida Gators etc would be 'real' views (i.e. non-master pages).
The tricky part is telling any parent master page which navigation item is currently selected. Because you can't bind master pages to the ViewModel you'll have to use the View Dictionary e.g. View["SelectedMainNavItem"].
Why not use some global layout template that always displays the main navigation, and relies on some helper to render the subnav? (The helper may be superfluous -- you might just output the subnavigation inline in the layout template)
Your controller passes current category/sub-category, and some data structure describing the current subnavigation options, to the view.
After contemplating this issue for a while along with the suggestions, I came up with this solution. Since my subnavigation will always be below the main navigation, I decided to go with the Convention over Configuration method.
In my Site.Master, I have the following two render partials. One displays the main navigation and the other makes a call to BuildSubNavigation to display get the name of a partial to render:
<% Html.RenderPartial("_MainNavigation"); %>
<% var submenu = ViewContext.BuildSubNavigation();
if (submenu != null) {
Html.RenderPartial(submenu);
}%>
Granted, this could be thrown into a Helper, and I intend to do that, this is more explicit and aids in the understanding of the issue.
What this does is call the BuildSubNavigation method. It goes with the convention that if a controller is to have a specific sub navigation, there will be a partial in the form of "_Navigation" So in the spirit of the example, one partial would be "_SouthEasternConferenceNavigation" What I do is then check to see if the current view actually exists. If it does, I return the name, where it's then used to render the partial.
public static string BuildSubNavigation(this ViewContext vc) {
var controller = vc.RouteData.Values["controller"] ?? "";
var viewName = "_" + controller + "Navigation";
if (ViewExists(vc.Controller.ControllerContext, viewName, null)) {
return viewName;
} else {
return null;
}
}
And this is the method that checks whether the View actually exists against the current View Engine:
public static bool ViewExists(ControllerContext cc, string viewName, string masterName) {
if (ViewEngines.Engines.FindView(cc, viewName, masterName).View != null) {
return true;
} else { return false; }
}
I'm unsure if this is the best way to do this, but it's working rather well for a small project I'm currently working on.
Thanks for the answers!

Resources