0

I would like to store some user inputs into a cookie on the client side. Using MVC3/Razor. Data entered into several DropDownLists. Examople of one below:

<div style="float: left;">
  @Html.Telerik().DropDownList()
     .Name("name").BindTo((SelectList)@ViewBag.Filter1SelectList)
     .Placeholder("All")
     .ClientEvents(events => events
     .OnChange("onChangeFilter1"))
</div>

I created the following JavaScript in an attempt to store values as the dropdown values change.

 function onChangeFilter1(e) {
    var filter1 = document.getElementById('name').value;        
    '<%= ViewData["Filter1Value"]%>' = filter1.toString();        
}

The idea is to gather several filters from multiple dropdownlists. Data is filtered on submit button. Is this even possible? I saw only one example and copied it carefully but the above errors: Microsoft JScript runtime error: Cannot assign to '[string]'.

Chris Laplante
  • 29,338
  • 17
  • 103
  • 134
mad moe
  • 322
  • 3
  • 14
  • take a look to this http://stackoverflow.com/questions/6111232/can-you-assign-values-to-viewdata-from-the-client-using-mvc – Overmachine Mar 21 '13 at 16:00
  • @Overmachine tried the link. Does not seem to work. That is a return the controller, save the filter value in viewdata and even the model but it my page does not filter accordingly. Seems the model is correctly filtered but the page update from the JSON does not work. I – mad moe Mar 25 '13 at 16:35

1 Answers1

0

Try to put @ sign before ViewData, it will allow you to assign your filter value to the ViewData

@ViewData["Filter1Value"] = filter1.toString(); 
RinaMi
  • 49
  • 3
  • No, if you put that in a Razor page, it will just output the value of ViewData["FilterValue"] followed by the string " = filter1.toString();". You can do it by wrapping everything in a code block: `@{ ViewData["FilterValue"] = filter1; }` – BenderBoy Feb 17 '23 at 17:38