11

I have two action methods in my controller. I want both to return one view.

The reason is because I have conditional checks in my view that checks if a property is null then don't show a div and if a user clicks on load button then the same view needs to be returned by setting the property of the model and then the div is shown.

This logic is required for atleast 15 times and I want to create separate actions that return the model but one view. Is it possible?

1
  • if you want to return the same view with different viewmodels, i prefer to pass the action a parameter which allows me to distinguish how to return the given view rather than create seperate actions to return the same view... Commented Jan 8, 2012 at 21:40

2 Answers 2

18

You just have to state that view name in your return statement:

public ActionResult SomeAction()
{
    // do something
    return View("SharedView", viewModel);
}

If you have a view named SharedView, all actions in that controller can access it. If you want to share it among actions that are on other controllers, you can use the Shared folder in Views or state full path with the view name.

Sign up to request clarification or add additional context in comments.

1 Comment

Here is a msdn reference page for this, View Method
1
return View("viewName", data);


First parameter tells framework from which view to render and 2nd parameter is used to provide data for that view
Secondly You can use

return RedirectToAction("actionName",new{data=something});

1 Comment

Rick read the question carefully. its controller action. i.e two action methods in one single controller

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.