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");
Related
Is it possible for a single action method to render two different views subsequently. And if possible pick the user input from the first view and use it on the second view?
Yes, completely possible. And, it can be multiple views, or even a FileResult or other result type.
public ActionResult MyAction() {
if(/*logic here*/)
return View("ViewOne");
else
return View("ViewTwo");
}
Yes, it's possible. Just make sure your views have same view model.
To switch between views you can specify parameter:
return View("MyFirstView", viewModel);
or
return View("MySecondView", viewModel);
I have a fairly complex view that's almost the same for the create and edit functionality in MVC3.
Every time I change one I have to remember to make the same changes in the other.
Is there a way that I can share a view between create and edit. For example can I have two view files with different names and link them or is there another even better way.
thanks
Marcel
You could simply make a partial view with your form contents and include this partial view in your create and edit view. With that, you you are able the have some differences in your views (maybe headline "edit" / "create").
#Html.Partial("FormView")
On the other side, you could specify your view in your controller action.
public ActionResult Create()
{
return View("CreateEditView");
}
public ActionResult Edit()
{
return View("CreateEditView");
}
I have a partial view that shows a list of Categories. I'd like to put that partial view on any page, but I'd like to have it to call to the service and get a list of categories by itself without me having to do that in every controller action. Something like webforms in which you can put a code-behind on it.
For eg.
Actions
public ActionResult Index()
{
JobListViewModel model = new JobListViewModel();
model.Categories= jobService.GetCategories();
return View(model);
}
public ActionResult Details(int id)
{
Job job = jobService.GetJob(id);
return View(job);
}
I created a partial that will take the model.Categories model and display a list. As you can see, the Index page will work fine, but I do not want to call it again in the Details page. Is there a way to make my partialview call to the GetCategories() service by itself?
Use Html.RenderAction - that gives the partial view its own controller action.
You should also mark you partial action with the attribute [ChildActionOnly].
DVark,
As noted in the accepted answer, for your scenario, RenderAction is the most appropriate.
I thought I'd link a little article that distils my thinking on the topic (i.e. when to use RenderPartial vs RenderAction):
http://cbertolasio.wordpress.com/2010/09/21/mvc-html-renderaction-vs-html-renderpartial/
hope it helps
[edit] - as an aside. a year or so ago, i got myself into a few scrapes by not appreciating the power of RenderAction, in favour of RenderPartial. as a result, i had littered the shared view space with lots of partialviews in order to access them from a variety of sources. the moral of the story: know your 'territory' before planting your flag.
I am using PartialView object for all the CRUD operations in my project.
I would like to return from more than one action (Create/Read/Edit) always the same PartialView that I have previously binded to the correct model object.
How can I achieve this?
Thanks
You can specify, which (Partial)View you want to return from an action like this:
return PartialView("ViewName", viewModel);
If you want to use the partial view from different controllers, then put it in the Views/Shared folder.
Just add this to the end of every action that you want the partial returned from
return PartialView("PartialViewName", yourModelName)
Is there a mandatory relationship between a Controller Action and a View? I mean is it necessary to have a physical View (.aspx page) for each Action inside a Controller class?
There is no mandatory relationship between the Controller Action and a view. The controller is responsible for returning an ActionResult. The most usual way of doing this is by using a View, but they aren't hard wired. A view could be shared across Controllers for instance.
Also a Controller, can deal with the request purely on its own, returning a redirect, or a JSON result, or even its own html (though not recommended).
You can also return things like ContentResult in an action:
public ContentResult Index()
{
return Content("Foobar!");
}
If this was called directly, this would be similar to:
Response.Write("Foobar!");
Response.End();