user1013459
user1013459

Reputation: 11

How to pass data from a View to a Controller

In my View, I have a table pulling various data in. Initially it only shows the rows actionable by that user. However, there is also an option to show all rows. To achieve that so far, I have two hyperlinks above the table, with hrefs of "?isRequiredAction=true" and "?isRequiredAction=false". In the Controller, I have the following:

public ActionResult Index(bool? isRequiredAction){
string userId = User.Identity.Name;
bool ira = true;
if (isRequiredAction != null)
{
    ira = Convert.ToBoolean(isRequiredAction);
}
...
return View(model);

So, right now the Controller is getting its parameter from the querystring created by clicking those links. I'm not satisfied with this approach since I don't want to dirty up the URL with this query. Is there a simpler way of achieving what I'm asking? We would like to avoid turning the links into form objects if possible. Thanks.

Upvotes: 0

Views: 156

Answers (3)

carawan
carawan

Reputation: 508

Technically, I don't believe there is very appropiriate solution that fits your need because you don't want to make the link form object, nor "dirtying up" the URL. I suggest you to stick with your first approach because there is nothing wrong with dirtying the URL. By "dirtying" your URL, you can make link bookmarkable. If you are really concerned with it, then you can have two choices:

(1) Use Ajax. This way your URL will be unaffected; however, you lose bookmarkability.

(2) Use URL rewriting to make your URL "pretty". This approach, under the hood, equals to "dirtying up" the URL, but in a "pretty" way.

Upvotes: 0

goenning
goenning

Reputation: 6654

You can create two distinct custom routes, for example:

  1. www.yourapp.com/home
  2. www.yourapp.com/home/showall

After that, you can make both routes target the same action or create a distinct action for each of these routes. Your choice!

Upvotes: 0

AD.Net
AD.Net

Reputation: 13399

This MSDN article should help.

From view to controller, you can do a HttpPost or you can pass the data as parameters.

Upvotes: 3

Related Questions