user905284
user905284

Reputation:

MVC checkbox checked state issue

Good morning, I have a view that display's search results for customers. On top of the view i have a filter that has a few checkboxes. The user can select multiple checkbox items and press the filter results button. When the user presses the button it calls an action that filters the result. The page is also refreshed. My question now, how can i make the page remember what checkboxes are checked. Because when the results are returned the filter elements are reset. Thanks in advance.

Upvotes: 0

Views: 1404

Answers (3)

Shyju
Shyju

Reputation: 218722

You can keep your selected/checked checkbox details in session variable when you post the form (by clicking on the search button). In the HttpPost action,read the checkboxes which were checked and add that to a collection property of your viewmodel and send it back to the view. set the session variable values to null once you are done with it (after reading). in the view, use your viewmodel value to set the checked status of those checkboxes.

Another option is using ajax. When you click on search.Read your search criteria and make an ajax request to the action method. Return a partial view back and update only the div/table which shows the results of the search.

Upvotes: 0

Philipp M
Philipp M

Reputation: 1891

Depends on how long the input criteria should be remembered. You could also save it in your session via Session["customerCriteria"] = yourCriteria, but it would be easier to give an example if you had provided some code.

Upvotes: 0

Ravi Gadag
Ravi Gadag

Reputation: 15861

you can use TempData. TempData VS ViewBag VS ViewData

you used ViewData, After the redirect, the ViewBag & ViewData objects are no longer available

TempData is also a dictionary derived from TempDataDictionary class and stored in short lives session and it is a string key and object value. The difference is that the life cycle of the object. TempData keep the information for the time of an HTTP Request. This mean only from one page to another. This also work with a 302/303 redirection because it’s in the same HTTP Request. Helps to maintain data when you move from one controller to other controller or from one action to other action. In other words when you redirect, “Tempdata” helps to maintain data between those redirects. It internally uses session variables. Temp data use during the current and subsequent request only means it is use when you are sure that next request will be redirecting to next view. It requires typecasting for complex data type and check for null values to avoid error. generally used to store only one time messages like error messages, validation messages.

TempData["CheckedList"] = YourCheckBoxListValues; //in your controller

in your View

@{    
      var tempchkboxList = TempData["CheckedList"] as yourStronglyTypeClass;                
                        //or 
       var tempchkboxList = TempData["CheckedList"].ToString(); 
}

Upvotes: 1

Related Questions