Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I call a controller action from within my view using Razor?

I have 2 Controllers

- HomeController
    - Index()
- AccountController
    - Login()

In my Home/Index.cshtml I want to Load The AccountController/Login method which then returns a view and displays it in my Home/Index view.

Home/Index.cshtml

<div class="row-fluid">
    <div class="col-md-12">
        <!-- Render the view that the AccountController/Login method denotes -->
    </div>
</div>

How do I do this?

like image 486
Jimmyt1988 Avatar asked Nov 08 '13 22:11

Jimmyt1988


People also ask

How do you call a controller method from Razor view?

We can use @html. action to call controller method.

How do you call a controller method from partial view?

The Partial Action method is decorated with ChildActionOnly attribute. The ChildActionOnly attribute is used to make sure that an Action method is only called from inside the View and cannot be access directly by the User. Generally it is used for Action methods invoked by Partial Views.


1 Answers

use this with your actual Controller/View names

@Html.Partial("../Home/Login", model)

or

@Html.Action("action", "controller", parameters)
like image 143
Kamil Budziewski Avatar answered Oct 21 '22 04:10

Kamil Budziewski