0

I have an action

public ActionResult Index(string search)
{ 
    var listingModel = Repository.Search(search); //gets from DB items. Model doesn't include 'serach' string as property or field
    return View("Index", listingModel)
}

The View contains filter area with text box (this code from extension method for helper):

htmlHelper.TextBox("Search", null, htmlAttributesDictionary);

I've noticed that it automatically inserts value of 'search' parameter into textbox. But my problem is that I cannot find how it gets it. I cannot see the source code of .TextBox() helper. I cannot find this value in htmlHelper.ViewData.Model. I want to change value of search parameter (I need to save it is Session and gets from session). Do you know what I can write in the controller action that will automatically sets my value from session into the checkbox.

I know that I can just use redirect, but it doesn't seem to be correct way. It looks for me not very beautiful:

public ActionResult Index(string search)
{ 
    search = GetFromSession();
    return RedirectToAction("Index", new {search = search});
    var listingModel = Repository.Search(search); //gets from DB items. Model doesn't include 'serach' string as property or field
    return View("Index", listingModel)
}
user2216
  • 809
  • 1
  • 8
  • 24
  • Just add `ModelState.Clear();` before `return View(...);` - to understand the behavior, refer [this answer](https://stackoverflow.com/questions/26654862/textboxfor-displaying-initial-value-not-the-value-updated-from-code/26664111#26664111) –  Jun 26 '18 at 09:23
  • @StephenMuecke Actually you comment was helpful, I've made some more tests and undersood how it works! thanks – user2216 Jun 26 '18 at 12:14
  • @StephenMuecke sure linked answer. I meant the comment and all its content – user2216 Jun 26 '18 at 13:18

0 Answers0