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.
Related
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")
If I have the controller HomeController and action Index(), but I have my template in Views/Index.cshtml as opposed to Views\Home\Index.cshtml - is there a way for me to bypass the conventional loading mechanism to render the former?
Yes, you can explictly tell in the View method from where to load the view. You just need to start your viewName parameter with ~/Views and you also have to write out the .cshtml extension:
public class HomeController : Controller
{
public ActionResult Index()
{
return View("~/Views/Index.cshtml");
}
}
However the MVC convention is that if you have views which don't belong to one specific controller then these views should go to the Views\Shared folder and from there they will be looked up.
i am new in mvc. due to lack of knowledge i am not being able to do one thing.suppose i have view Index.cshtml and this view reside in home folder. i have register folder in home folder and in register folder there is view called register.cshtml. i have another folder called catalog in home folder. when i will run my application then by default Index view will render and there will be two button or two link button. one is button text is Catalog and another button text is register.
when user click on register button then register view should load and when user click on Catalog button then Catalog view should load. how could i do this ? what kind of code i need to write and what kind of code i need to write for mapping in global.asax file ?
another question is that how could i pass my model or view model too when navigate from one view to another view.
looking for help & concept with sample code. thanks
Considering your question, I've come up with the idea that your knowledge about web applications comes from ASP.NET that folders are used to categorize different area in a web application. If I were right, you should map folders in ASP.NET with Controllers In ASP.NET MVC (it is not good analogy, but for starting is helpful). In this way, you would have three Controllers or one Controller with three Actions. I am going to choose second one.
Controller:
public class HomeController : Controller
{
public ActionResult Index()
{
var model = new TheViewModel();
return View(model);
}
public ActionResult Register()
{
return View();
}
public ActionResult Catalog()
{
return View();
}
}
View:
#model MvcApplication1.ViewModels.TheViewModel
#{
ViewBag.Title = "Index";
}
<h2>Index</h2>
#Html.ActionLink("Register", "Register")
<br/>
#Html.ActionLink("Catalog", "Catalog")
Your second question has answered at Passing ViewModel in ASP.Net MVC from a View to a different View using Get
Call the appropriate /controller/action in your respective button click handlers.
In your case for the register button handler direct it to /home/register.
Have a view for your register functionality.
In the register action of your home controller return the view you want to show.
public ActionResult Register()
{
return View();
}
so in my profile controller page.
I have a method call create
inside the create method
if (Convert.ToInt32(calBMI) >= 25)
{
return View("Index", Survey);
}
I want to render the page to index of survey(survey is another controller take care of surveys), how do i do it to get it works,thanks!!
return View("~/Views/Survey/Index.cshtml", objSurvey);
Assuming objSurvey is your model/ViewModel object and Survey/index view is strongly typed to the type of objSurvey Model/ViewModel
EDIT : As per the comment, If your view is not strongly typed, you can ignore the second parameter
public ActionResult GetSomeThing()
{
return View("~/Views/Survey/Index.cshtml");
}
If your intention is to share this view among multiple controllers, it should be in the /Views/Shared/ folder. There is a lack of good reasoning to use a view outside of either the controller folder or the shared folder.
It seems to me that you can just redirect to list of surveys (if that's your intent).
return RedirectToAction("Index", "Survey");
I have a view file structure like:
Views
Company
Department
Employee
ManageEmployee.cshtml
and the controller is
public class EmployeeController : Controller
{
public ActionResult Index(int dptId)
{
var loadedEmp = getEmpOf(dptId);
return View("Company/Employee/ManageEmployee", loadedEmp);
}
}
But the controller give me an error - telling that it can't find the view.These are the paths it search.
~/Views/Employee/Company/Employee/ManageEmployees.aspx
~/Views/Employee/Company/Employee/ManageEmployees.ascx
~/Views/Shared/Company/Employee/ManageEmployees.aspx
~/Views/Shared/Company/Employee/ManageEmployee.ascx
~/Views/Employee/Company/Employee/ManageEmployee.cshtml
~/Views/Employee/Company/Employee/ManageEmployee.vbhtml
~/Views/Shared/Company/Employee/ManageEmployee.cshtml
~/Views/Shared/Company/Employee/ManageEmployee.vbhtml
Basically if I'm able to eliminate the Employee section, the engine will find it.
~/Views/Employee/Company/Employee/ManageEmployee.cshtml to this
~/Views/Company/Employee/ManageEmployee.cshtml
Any insights on how to achieve this.
Thanks.
Have you tried:
return View("/Company/Employee/ManageEmployee", loadedEmp);
It looks like the engine is trying to return the view relative to your current location in the site rather than from the root of the site.
View has to be returned from the controller in the following way (for Specific View):
return View("ManageEmployee", loadedEmp);
In MVC, the controller will automatically route to the View name you provided.
loadedEmp should be the object you are passing to the view.
You need to follow MVCs convention of ControllerNameController for your controller and your view structure of ControllerName/...
If you want full control over your structure you'll need to switch to a different framework like FubuMVC.
If you want your own convention of arranging the views folder structures, it would be better you plug in your own view engine.