Rick Hodder
Rick Hodder

Reputation: 2252

MVC4 passing same parameters to multiple actions on controller

I am creating an MVC4 application that has an index page that shows a grid. The index action for the page has parameters for a search string, sort order, account id, and vendor id that control what is shown.

public class MyController
{    
        public ActionResult Index(string searchFilter="", 
                                  string sortOrder="createddate", 
                                  int accountid=0, 
                                  int vendorid=0)
        {
            ...
        }

}

From this main page I can click on rows in the grid, which take me to the create and edit actions. In addition, I have several other actions on the control that are called from the index page.

When I press Save or Cancel on any of these views (the [HttpPost] action, as usual they redirect to the index action. But these views don't have the search string, sort order, account id, and vendor id so that the index view can "restore" what was being shown.

So I have been adding these parameters to the other actions methods, and making an object with the properties needed to in the view models for the methods.

public class PageFeatures
{
     public string SearchFilter {get; set;}
     public string SortOrder {get; set;}
     public int AccountId {get; set;}
     public int VendorId {get; set;}
}

public class CreateViewModel
{
      public PageFeatures Features { get; set; }

}

public class EditViewModel
{
      public EditViewModel(int id)
      {
           Id=id;
      }
      public int Id {get; set;}
      public PageFeatures Features { get; set; }

}

public class MyController
{    
        public ActionResult Index(string searchFilter="", 
                                  string sortOrder="createddate", 
                                  int accountid=0, 
                                  int vendorid=0)
        {
            ...
        }

        public ActionResult Create(string searchFilter="", 
                                  string sortOrder="createddate", 
                                  int accountid=0, 
                                  int vendorid=0)
        {
            var model = new CreateViewModel();
            model.Features = new PageFeatures 
            {
                SearchFilter = searchFilter,
                SortOrder = sortOrder,
                AccountId = accountid,
                VendorId = vendorid
            }

            return View(model);
        }

        [HttpPost]
        public ActionResult Create(CreateViewModel model)
        {
              if(ModelState.IsValid)
              {
                 ....
                 return RedirectToAction("Index", new {searchFilter=model.Features.SearchFilter,
                                                       sortOrder = model.Features.SortOrder,
                                                       accounted = model.Features.AccountId,
                                                       vendorid = model.Features.VendorId} );
              }
        }

        public ActionResult Edit( int id=0,
                                  string searchFilter="", 
                                  string sortOrder="createddate", 
                                  int accountid=0, 
                                  int vendorid=0)
        {
             var model = new EditViewModel(id);
            model.Features = new PageFeatures 
            {
                SearchFilter = searchFilter,
                SortOrder = sortOrder,
                AccountId = accountid,
                VendorId = vendorid
            }

            return View(model);
        }

        [HttpPost]
        public ActionResult Edit(EditViewModel model)
        {
              if(ModelState.IsValid)
              {
                 ....
                 return RedirectToAction("Index", new {searchFilter=model.Features.SearchFilter,
                                                       sortOrder = model.Features.SortOrder,
                                                       accounted = model.Features.AccountId,
                                                       vendorid = model.Features.VendorId} );
              }
        }

}

But what I don't like about this approach is that I am peppering my views and every controller method with these values.

Am I doing the right thing, or is there a better way, like an action filter that would make this stuff easier and less repetitive?

Upvotes: 0

Views: 995

Answers (2)

Rajshekar Reddy
Rajshekar Reddy

Reputation: 18987

[HttpPost]
        public ActionResult Edit(EditViewModel model)
        {
              if(ModelState.IsValid)
              {
                 ....
                 return RedirectToAction("Index", new {searchFilter=model.Features.SearchFilter,
                                                       sortOrder = model.Features.SortOrder,
                                                       accounted = model.Features.AccountId,
                                                       vendorid = model.Features.VendorId} );
              }
        }

You are using the right approach.

If you dont want your URL to be ugly filled with query string parameters. You can try storing these values in a session and retrieving them in each action of your controller. Set them once and your done to use it anywhere, if you want to change the session values you can just over write it

Upvotes: 1

Gjohn
Gjohn

Reputation: 1281

Have you thought about using TempData and setting these params in there. The best part is that you can persist TempData across Controllers and you can perhaps modify the TempData values in a base controller class.

Upvotes: 1

Related Questions