How to connect an existing view to a controller action?

How can I attach an existing view to an action? I mean, I’ve already attached this “View to Action”, but I want it to be attached to the second action.

Example: I have an action named Index and a view, the same name attached to it, right-clicking, adding a view ... but now, how to connect to the second? Suppose an action called Index2, how to do this?

Here is the code:

//this Action has Index View attached
public ActionResult Index(int? EntryId)
{
   Entry entry = Entry.GetNext(EntryId);

   return View(entry);
}

//I want this view Attached to the Index view...
[HttpPost]
public ActionResult Rewind(Entry entry)//...so the model will not be null
{
   //Code here

   return View(entry);
}

I googled and cannot find the correct answer ... Is this possible?

+5
source share
2 answers

"" , , , Controller.View Method

public ActionResult MyView() {
    return View(); //this will return MyView.cshtml
}
public ActionResult TestJsonContent() {
    return View("anotherView");
}

http://msdn.microsoft.com/en-us/library/dd460331%28v=vs.98%29.aspx

+5

? View :

 public class TestController : Controller
{
    //
    // GET: /Test/

    public ActionResult Index()
    {
        ViewBag.Message = "Hello I'm Mr. Index";

        return View();
    }


    //
    // GET: /Test/Index2
    public ActionResult Index2()
    {
        ViewBag.Message = "Hello I'm not Mr. Index, but I get that a lot";

        return View("Index");
    }


}

(Index.cshtml):

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>@ViewBag.Message</p>
+4

All Articles