Action-View binding: magic! - asp.net-mvc

Say there's HomeController with an Details-action. return View() will send data to the Detals.aspx in the Home folder. But who makes that binding? and what if I want it to go to Edit.aspx instead?
Background:
Alot of the code in Details.aspx and Edit.aspx is identical, save for one textbox. Maybe by MVC rigor, the view is not supposed to make that kind of decisions, but hey, there's got to be a limit.

You can make it go to Edit.aspx by specifying it as a parameter of the View() function.
return View("Edit");
As to who makes the actual binding happen, it's the View Engine. It receives the returned ViewResult and analyzes it to see which template file to load and display. When it gets the string "Edit", it runs a find routine, using the context of the controller, to search a number of directories for filenames that match the convention. It starts in the controller's View directory, and then searches the Shared directory.

If you want to Edit.aspx to be rendered you could return View("Edit");

Related

¿Relation between Controller's Action Method and the Views?

I've a couple conceptual questions about the relationship between the Controller Action Method and the View:
I see that the name of the view is in function of Controller's name; the name of the controller class is the same of the namespace of the view (represented by the name of the folder where is the .cshtml file) and the name of the method is the same name of the .cshtml file which is the view. Why it works so? If I miss something in the explanation, How it exactly work?
Is there a way where I can put the .cshtml files (views) outside from the folder which match the name of the Controller's class name?
Is a restriction from MVC that names of .cshtml files match the name of the controllers action methods?
What exactly is the View() method which is returned in the controller action method?
Why the methods of the controller class are called "Controller Action Methods"?
Is a restriction from MVC that the names for the controllers end with "Controller" word?
Thanks to all
I would highly suggest running through a tutorial on MVC, such as this codeproject tutorial.
Naming convention are not set in stone, but in general allows for an understandable structure.
Yes, to navigate to a View you can specify the path. return View("~/this/is/your/path/ViewName.cshtml");
No. See above, you can return any view from any methods with the correct path, assuming the return type fits the return type of the method (Explained below).
View() is a C# "method" which tells the project to navigate to the View you specify, along with any parameters you pass. A more correct and in depth answer can be found by looking at the docs. The default return View() attemps to navigate to the return View("MethodName");
Action methods are methods with a return type of ActionResult. Redirect and View are examples of such. Think of them as similar to object return types, such as void or string, but instead ActionResult will tell your project to do something, such as redirect to another method or return a ViewResult via View().
No. Try it! It is however usually good practice, as it will easily separate your files by name, since you will likely have similar filenames n your project.

What is difference between #Html.RenderPartial("_Common.cshtml") and #Html.RenderPartial("_Common")

I am facing one problem in MVC4. If I am not providing .cshtml while calling RenderPartial then it is not calling Partial View.
For Example
#Html.RenderPartial("_Common.cshtml") //it is working
#Html.RenderPartial("_Common") //it is not working
My question is why it is not working?
As Zabavsky mentions, you may have an incorrect filename on your partial view (double extension). Just did that yesterday on a project, so easy to do, but I think you should be using Partial and not RenderPartial
Just to clarify which options should work:
1) The extension of the view file is required if you supply a path.
2) If you do not supply a path, do not supply the extension.
The examples below assume cshtml files.
Use RenderPartial in a code block:
// This looks in default view folder, then shared, checking for .aspx, .cshtml etc
Html.RenderPartial("DefinitionDetails");
// This looks in specified path and requires the extension
Html.RenderPartial("~/Views/Definition/DefinitionDetails.cshtml");
Use Partial for inline Razor syntax:
// This looks in default view folder, then shared, checking for .aspx, .cshtml etc
#Html.Partial("DefinitionDetails")
// This looks in specified path and requires the extension
#Html.Partial("~/Views/Definition/DefinitionDetails.cshtml")
Note: Apparently RenderPartial is slightly faster than Partial, but I also expect fully pathed names will be a faster than letting MVC search for the file.
If you are producing partials in a loop (i.e. from a collection in your view model), it is likely you will need to pass through specific viewmodels:
e.g.
#foreach (var group in orderedGroups)
{
Html.RenderPartial("~/Views/ControllerName/ViewName.cshtml", group);
}

Razor: #Html.Partial() vs #RenderPage()

What is the appropriate way of rendering a child template?
And what's the difference? Both seem to work for me.
And why does #Html.RenderPartial() no longer work?
Html.Partial("MyView")
Renders the "MyView" view to an MvcHtmlString. It follows the standard rules for view lookup (i.e. check current directory, then check the Shared directory).
Html.RenderPartial("MyView")
Does the same as Html.Partial(), except that it writes its output directly to the response stream. This is more efficient, because the view content is not buffered in memory. However, because the method does not return any output, #Html.RenderPartial("MyView") won't work. You have to wrap the call in a code block instead: #{Html.RenderPartial("MyView");}.
RenderPage("MyView.cshtml")
Renders the specified view (identified by path and file name rather than by view name) directly to the response stream, like Html.RenderPartial(). You can supply any model you like to the view by including it as a second parameter
RenderPage("MyView.cshtml", MyModel)
I prefer
#RenderPage("_LayoutHeader.cshtml")
Over
#{ Html.RenderPartial("_LayoutHeader"); }
Only because the syntax is easier and it is more readable. Other than that there doesn't seem to be any differences functionality wise.
EDIT: One advantage of RenderPartial is you don't have to specify the entire path or file extension it will search the common places automatically.
The RenderPartial method doesn’t return HTML markup like most other helper methods. Instead, it writes
content directly to the response stream, which is why we must call it like a complete line of C#, using a semicolon.
This is slightly more efficient than buffering the rendered HTML from the partial view, since it will be written to the
response stream anyway. If you prefer a more consistent syntax, you can use the Html.Partial method, which
does exactly the same as the RenderPartial method, but returns an HTML fragment and can be used as
#Html.Partial("Product", p).
We can also pass model using partial views. #Html.Partial("MyView","MyModel");
#RenderPages()
The above does not work in ASP.NET MVC. It only works in WebPages.
#Html.Partial("_Footer")
You will need to use the above in ASP.NET MVC.
For ASP.NET Core 7. In the Shared folder make partial file then user this following code
<partial name="_NavBar" />

asp.net mvc how it decides which view to load

I am attempting to construct an asp.net mvc app which will use the urls like:
/Controller/[Number]/Action/Id
I have got it to always call my controller and pass it the Number and the Id fine...
However I now want to return a different view depending on the Number
I could have options like:
if([Number] == 1) { return View("ViewName");}
if([Number] == 2) { return View("ViewName2");}
however I instead was wondering if there was a way to change the core so that instead of searching at ~/Views/controller/action.aspx I could have my own method which did some checking on the Number then passed to the virtual file provider is a different path
Hope this makes sense!
Decide which view to load, depending on input parameters is a controller task. You could write your own view engine.
But it is easier to return the full path to the view you want to return.
return View("~/myviews/ViewName3.aspx");
This will render ViewName3 from given directory.
You might want to look at decorating your controller method with Action Filter Attributes.
Then, you could do something special inside the Action Filter Attribute.
Or, you could pass Number to a Model object, then have the model Object return the right View path.
Either way, your instinct of trying to keep too much logic out of the Controller is sound, especially if [Number] is somehow a business concern and not a view concern.
You need to look into / google creating a custom view engine.
By the sounds of things you probably just want to extend the built-in WebFormViewEngine and just override the locations and the .FindView() method.
HTHs,
Charles

Obtain the pathName dynamically for ASP.NET MVC

I'm looking for a solution that allows me to get the parent page pathname from a partialView invoked. For example I have an Action baa and a controller boo so the javascript to obtain this will be window.location.pathname, and it returns /boo/baa.
The thing is that I use a partial view, so when I try to retrieve the pathname via the URL helper, what I obtain is the PartialView one, and what I need is the pathname for the action that invokes it. I want to do this because I want to show certain things of the partial view depending on the pathname of the parent view that invokes it. Is there a way to do this with ASP.NET MVC?
Thank you all in advance,
You can access the RouteValues on the ViewContext on the page. This will contain the controller and action values as determined by the routing engine. You might want to consider making the determining information part of your model, however, as this exposes logic in the controller -- what the view does with this information, though, is completely up to it.

Resources