ltect
ltect

Reputation:

Can I share a view in asp.net mvc?

My controller has 2 actions:

I want to share the view named index.aspx between these 2 actions.

See my previous post for more information

When I build my link to the page, I assume I cannot send it to Index action as it is expecting a FormCollection type and hence I create a Results action

public ActionResult Results(ClientSearch data, int? page)
    {
        FormCollection collection = new FormCollection();
        collection.Add("FNAme", data.FName);
        collection.Add("Lane", data.Lane);
        collection.Add("Zip", data.Zip);
        collection.Add("Phone", data.Phone);


        return Index(page, collection);
    }

Upvotes: 1

Views: 120

Answers (2)

user151323
user151323

Reputation:

Of course you can. It's up to controller to decide how to react and what view to serve back.

Now that I've read your question to the end :)), well, you can get away with two actions of the same name. The one will be accepting GET commands (initial load of the page), the other will be serving POST requests, perform the necessary action and redirect back to the same View.

public MyController
{
    [AcceptVerbs (HttpVerbs.Get)]
    public ActionResult Index ()
    {
        return View ();
    }

    [AcceptVerbs (HttpVerbs.Post)]
    public ActionResult Index (ClientSearch data, int? page)
    {
        // Process form post

        return RedirectToAction ("Index");
    }
}

Upvotes: 3

swilliams
swilliams

Reputation: 48910

Not sure I completely understand your question, but if you want to use the same View on different ActionResults, you can:

public ActionResult One() {
  // do stuff
  return View("Index", myModel);
}


public ActionResult Two() {
  // do stuff
  return View("Index", myOtherModel); // Same View
}

Just make sure you are providing the same Type for the View (if the View needs a Type at all).

Upvotes: 4

Related Questions