how to check view associated with which controller ? - asp.net-mvc

I just get assigned to a ASP MVC project.
It has lots of controllers and lots of views...
I am getting confused abt which view is associated with which method of a controller ?
How to check which controller and method is associated with view ?
or
for which controller view has been added ?

Right click from the view; select go to controller

If you want to know the view of a method in a controller, right click on the method name. That is if public ActionResult Index(string returnUrl) is your method of a controller, right click on the Index, then you can see the option Go To View. Click on that to go to the view of that method. Hope this helps..

Well,
ASP.NET MVC, follows a "convention over configuration" thumb rule.
so unless you have configured something special in your project, it should follow a convention.
The convention is, every controller action would have a view with the same name.
I.e. If we have a controller of name "Users" with a controller method,
public ActionResult MyView()
then the corresponding View would be named MyView.aspx or MyView.shtml or MyView.cshtml inside a folder named "Users".
hope this answers your question.

There is many way to create URL in MVC.
1 Configure Route in a file and assign controller name and action
2 you can check URL and find the Controller and action. ie. http://www.abc.com/customer/address In this Controller name is "customer" and action name is "address"
Using action name, you can find View name is define or not if there is no view name its means view name is similar with action name under controller name folder.

Related

Html.BeginForm does not redirect to action Method

I try using Html.BeginForm to submit my data but it does not help me with it. I followed this link to make my mvc program: https://www.c-sharpcorner.com/UploadFile/f82e9a/form-data-submiting-and-displaying-data-another-view-using-m/
#using (Html.BeginForm("SubmitEmp","ClientController",FormMethod.Post))
using this I get the following error:
My view has a folder named Client, in which there are two files:
Index: this contains the form.
this the view to get the details entered in the previous form.
My controller (ClientController) has the code mentioned in the link
and my Model is Cmodel with the same code.
Your Code should be like this then it will work
#using (Html.BeginForm("SubmitEmp","Client",FormMethod.Post))
We cannot and use full controller name like ClientController for specifying controller name we need to specify only the name of Client
In your URL localhost:5000/ClientController/Submitemp showing controller name ClientController it should be Client only change your URL like localhost:5000/Client/Submitemp then it will work.
Cheers!!
No need to pass "Controller" in controller name, simply pass "Client". Make sure you have "SubmitEmp" HttpPost action in Client Controller
I can see that you have passed controller name as 'ClientController' which appears to be incorrect. The Controller in 'ClientController' represents that this is a Controller named Client. You can pass 'Client' instead of 'ClientController' in the place for controller.

¿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.

Rendering action from base controller, getting route not defined error

I have a base controller all my other controllers inherit from, inside the base controller are 2ActionResult that is common to every page on my site. When I try to use Html.RenderAction to call either of these actions from a View I get the error
No route in the route table matches the supplied values.
I have checked and the Action is set to public and the names are spelled right. What am I doing wrong?
The call: #{ Html.RenderAction("SectionNavigation"); }
The Action:
[Route("SectionNavigation")]
public ActionResult SectionNavigation()
As you can see I am also using Attribute Routing as well. I also tried updated the call to specify Base as the controller but that did not change anything.
The first parameter of the RenderAction method is the action name, not the route name.
If you're rendering a View that is being generated using a Controller other than the one that defines the SectionNavigation Action, you'll have to specify the Controller name as well:
#Html.RenderAction("SecondNavigation", "YourControllerName").
If you're using Areas, you'll have to include the Area name as well:
#Html.RenderAction("SecondNavigation", "YourControllerName", new { Area = "MyAreaName" }).
See Documentation

MVC architechture views and controller

You can tell which controller is serving the view, by looking at the code, and also the url in asp.net.
is there any other way to tell which controller is creating the view besides the two way i mentioned?
View depends on Actions in Controller. For example if you have a controller and the Action like below,
public class HelloWorldController : Controller
{
public ActionResult Index()
{
return View();
}
and in the Views/HelloWorld folder a file Index.cshtml then you can say Index.cshtml view is for Index action of HelloWolrd Controller. It works as follows,
There would be subfolder with the name of the Controller in the Views Folder
There would be actionname.cshtml file representing view of a particular action
Hope it helps
A nice, short snippet, to fetch the controller name in the view, is this one:
#ViewContext.RouteData.Values["controller"]
Just use it in your view, where needed.

MVC - How to include a model inside _Layout for Partial View

I've seen how to use Partial Views from within a View and I understand how a model is passed from the View to a Partial View. What I don't grasp is how to include a Partial View inside of _Layout.cshtml so that I can pass it to the Partial View - or, how the Partial View itself can call a Controller Action to create a Model for use.
Specifically, I want to include a Partial View within _Layout.cshtml to display in the header of every page. The Partial View will display a custom setting in the User Profile. I can't think of any way to obtain the Model without making a Controller Action call - but how is this done in/for a Partial View from within _Layout.cshtml?
Is my only option of accessing a Controller Action to build my Partial View's required Model to use a jQuery call? Or is there another way?
The answer is by calling #Html.Action().
It sounds like you're on the right track.
The key is to try to not pass multiple models around, make models complicated, or use the ViewBag needlessly.
Instead, any time you want information on every page, call an action from your _Layout.
For example, you might have a controller called PartialsController or SharedController.
public class PartialsController : Controller
{
[ChildActionOnly]
public ActionResult UserProfilePartial()
{
UserProfileModel model = new UserProfileModel();
return PartialView("_UserProfile", model);
}
}
The ChildActionOnlyAttribute means that users cannot access the action directly. Only your code can call the action. You can also apply the attribute to the controller so that it affects all actions automatically.
Now call the action from your view (_Layout).
#Html.Action("UserProfilePartial", "Partials")

Resources