asp.net mvc - MVC C# Retrieving selecteditem intoDropdownlist on page load -
I have a view and a button dropdown list. Now when I click on the button, the value of this dropdown list value According to the data loads. The filtering works and displays the page fully but the dropdown list value keeps the setting again. I want to retrieve this last selected value.
This is a view with my page AllDates.cshtml and will filter the buttons and return the same page again:
@ {ViewBag. Title = "all cars"; } & Lt; Form name = "filter" action = "~ / home / alldates" method = "post" & gt; & Lt; Select id = "fly" name = 'fly' & gt; & Lt; Option value = 'any' & gt; Any & lt; / Options & gt; & Lt; Option value = 'plane' & gt; Aircraft & lt; / Options & gt; & Lt; Option value = 'kyte' & gt; Kyte & lt; / Options & gt; & Lt; / Select & gt; & Lt; Input id = "refine" type = "submit" value = "refine" /> Here is my controller. Ive been asked to call the controls using the request, that it is not a good option, but I am new to MVC and it seems that this Its difficult. All public indexed data () {session ["fly"] = request ["fly"]; Var allDates = MyObjectToPreview; See Return (allDates); }
This is working very well, but once the scene is loaded, what do I do if I want to get ["fly"] in the "drop" selected item? should do?
First of all, you do not need to save it on session
. This is an unnecessary step that does not buy you anything. Second, you are manually defining your select
, which means that there is nothing to set the selected value for the razor. If you manually create select
, you are also responsible for setting the selected value manually, for example:
Razors are a good way to handle the selection list for you:
@ html.DropDownList ("Fly", the new list & lt; SelectListItem & Gt; {Since razor is involved in the creation of HTML, now it can choose the correct option. ["Fly"]
.
/ Html>
Comments
Post a Comment