Associate a controller to an existing view

Asked

Viewed 437 times

0

Usually when we create a Controller and associate a View to a Action, right click on Action and Add View. Then he creates the View, the folder and etc.

Now, and when there already is one View, and I want to associate a Action to that View? How do I do it? With the right there is no option Add view existing or something like it. How I associate?

2 answers

2

Just change the return of your function, and to name the view that you want:

public ActionResult NomeFuncao()
{
    // código
    return PartialView("_NomeViewPartial");
}

0

You can set the return of an Action to the desired View:

public ActionResult NomeAction(SeuModel seuModel)
{
    // Seu código aqui
    return View("SuaView", seuModel);
}

Besides in your View you can also define which Action will be called in the Submit of your <form>:

@using (Html.BeginForm("NomeAction", "NomeDoController", FormMethod.Post))
{

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.