0

I really need to maintain this string called "filterParams" in my MVC application. After the user enters some search parameters he clicks submit, and the grid is rebinded with that parameter. That works great. I also save the filterParams data in a Javascript variable, so when the user pages, and the OnDataBinding event is raised, the filter is also passed through that ajax call as well. this is all well and good however there is a huge issue, because when the user updates a question, all the results dissapear because it returns to the View and it does not have any data there. The way I'm using ViewData isn't working, and I could use your help, because if I can store it in ViewData and access it, it would fix my problems. I cannot use TempData because there are a number of other Actions that can be called in between Select and Update...Long question short, how do I implement ViewData correctly to store and retrieve a string in my controller?

Here are some code snippets.

 [GridAction]
    public ActionResult GetAllQuestion(string filterParams)
    {
        var _filterParams = new List<string>();
        _filterParams.Add(filterParams);
        ViewData["filterParams"] = _filterParams;
            return View(new GridModel(QuestionManager.Instance.GetQuestion(filterParams)));
    }



 [GridAction]
    public ActionResult EditQuestion(int id, QuestionDTO pQuestion)
    {
        //   var _question = QuestionManager.Instance.GetQuestion(id,false).FirstOrDefault();
        // TryUpdateModel(_question);
        var _filterParams = (List<string>)ViewData["filterParams"];
        var filterParams = _filterParams[0]; 
        QuestionManager.Instance.UpdateQuestion(pQuestion);
       // return View(new GridModel(QuestionManager.Instance.GetQuestion(id, false)));
        return View(new GridModel(QuestionManager.Instance.GetQuestion(filterParams)));
    }

in my aspx page

Html.Telerik().Grid<QuestionDTO>()
.DataBinding(dataBinding => dataBinding.Ajax().Select("GetAllQuestion", "Question", new { filterParams = string.Empty }).Update("EditQuestion", "Question").Insert("CreateQuestion", "Question"))

How can I get this to work please? Help is appreciated

egucciar
  • 2,039
  • 6
  • 23
  • 24
  • You can use Session if you want to persist something across a number of requests. – ngm Apr 30 '12 at 21:10

3 Answers3

1

ViewBag/ViewData only works for sending data from an action to a view. It does not get populated by the Model Binder when a request is made to an action, and its state is not saved between requests because ASP.net MVC is entirely stateless. In other words, the ViewData dictionary is always empty at the start of a request.

Meaning this line in your EditQuestion action will not work:

var _filterParams = (List<string>)ViewData["filterParams"];

ViewData is empty, so _filterParams will be null.

You have to manually send filterParams to the EditQuestion action just as you do for the GetAllQuestions action.

Perhaps a better alternative would simply be to persist filterParams using a temp cookie on the client side.

Uchendu Nwachuku
  • 442
  • 3
  • 13
0

So, just to defy all the misinformation I've read on the subject, TempData infact does persist through multiple action calls in the controller and was able to be used to implement the functionality I needed.

egucciar
  • 2,039
  • 6
  • 23
  • 24
0

Why just not store the data in Session? Here's a good explanation with examples http://rachelappel.com/when-to-use-viewbag-viewdata-or-tempdata-in-asp.net-mvc-3-applications

coffeeyesplease
  • 1,018
  • 9
  • 15