0

I have done some reading and I can't figure out how I change the view, by using the controller to handle the changing of the view. I have 2 buttons that currently use onclick to change the view but I want it done via the controller.

This is my view:

@{
    ViewData["Title"] = "RoleCreator";
}
<div class="ui-layout-center">
    <h1>Example Header</h1>

    <p>Example Text </p>


    <input type="button" value="Create Role"  onclick="location.href = 'RoleManager/RoleCreator'"/>
    <input type="button" value="Edit Role"  onclick="location.href='RoleManager/RoleEditor'"/>
</div>
Ryan white
  • 19
  • 8
  • 1
    Don't hard-code urls in your `onclick`, use `Url.Action()` instead – haim770 Dec 05 '16 at 10:07
  • That is why i want it to be done via the controller, but i don't know how to have it done that way. Im going to assume Url.Action() wont do that? – Ryan white Dec 05 '16 at 10:08
  • 1
    What do you mean by that? The controller is going to be called anyway. – Antoine Delia Dec 05 '16 at 10:08
  • Well instead of the view being called like this i want it so that it sends, for example (Note i am new to mvc so im just speculating) i click the button, it sends an action that the controller then uses to change the view. Am i barking up the wrong tree or is this correcT? – Ryan white Dec 05 '16 at 10:10
  • In controller if you want to go to anther action use `return RedirectToAction('YourActionName', 'YourControllerName');` – Nadeem Dec 05 '16 at 10:16
  • How would i use the action in the view? Im finding MVC really confusing – Ryan white Dec 05 '16 at 10:22
  • Basically, the View is the place where you put your html. In here, you can add links to different pages and when the user clicks on them, it will automatically call the right controller which will then redirect you to your correct view. – Antoine Delia Dec 05 '16 at 10:37
  • Oh right, so using the Url.Action() instead of hard coding the link would be like hard coding a password instead of calling it from a database? (Poor example but im trying to get my head around it) – Ryan white Dec 05 '16 at 10:44
  • You could hard code it but it is better to use the method for it, in this case Url.Action – Antoine Delia Dec 05 '16 at 11:04
  • ok thanks, ill give it a try – Ryan white Dec 05 '16 at 11:07

2 Answers2

0

You can't change the view using the controller. To understand more about the MVC refer to this link and check the relationship between Model-View-Controller. https://msdn.microsoft.com/en-us/library/dd381412(v=vs.108).aspx

To answer your question you may want to read more about partial views and ajax.

https://www.codeproject.com/articles/698246/asp-net-mvc-special-views-partial-view-and-layout

Using Ajax to render MVC View

Community
  • 1
  • 1
  • I thought it was possible by using actions? – Ryan white Dec 05 '16 at 10:25
  • Action is used to process data, and to select what view it will render next. Basically what you want to do in actions is to process all your data and select a view that will display/render all those data. – Matthew Paulino Dec 06 '16 at 01:16
0

You can use conditions to send different views . give name property to buttons.

On controller side you can call action as per name

 [HttpPost]
        [ActionName("IndexPost")]
        [Button(ButtonName = "Command")]
        public ActionResult GetFareFamilies()
        {
            return View("View1");
        }
        [HttpPost]
        [ActionName("IndexPost")]
        [Button(ButtonName = "Save")]
        public ActionResult Save(AirPricingWithoutPNRViewModel pricingVm, PricingCommercialDetails commercials)
        {
             return RedirectToAction("Action name");
    }