简体   繁体   中英

Select dropdownlist value from session

I have a dropdown list in my html file.

I want the location (in the dropdown) to be automatically selected if it is present in the session object if session.

I hope you understand my question. I am new to mvc. Thanks

@if (Session["Location"] == null)
{
                <select class="form-control" id="DeliveryLocation" name="DeliveryLocation">
                    <option value="" selected disabled hidden>Choose Your Location</option>
                    <option value="Aundh">Aundh</option>
                    <option value="Baner">Baner</option>
                    <option value="Balewadi">Balewadi</option>
                    <option value="Pimpri">Pimpri</option>
                    <option value="Nilakh">Nilakh</option>
                    <option value="Wakad">Wakad</option>

                </select>
}
else
{
    // i want code here to select value from dropdown as it is in location session
   <pre> <select class="form-control" id="DeliveryLocation" name="DeliveryLocation">
                    <option value="" selected disabled hidden>Choose Your Location</option>
                    <option value="Aundh">Aundh</option>
                    <option value="Baner">Baner</option>
                    <option value="Balewadi">Balewadi</option>
                    <option value="Pimpri">Pimpri</option>
                    <option value="Nilakh">Nilakh</option>
                    <option value="Wakad">Wakad</option>

                </select>
}

100% work in your situation.

<select class="form-control" id="DeliveryLocation" name="DeliveryLocation">
    <option @(Session["location"] == null ? "selected disabled hidden": "")>Choose location</option>
    <option @((string)Session["location"] == "USA" ? "selected" : "")>USA</option>
    <option @((string)Session["location"] == "Praga" ? "selected" : "")>Praga</option>
    <option @((string)Session["location"] == "Paris" ? "selected" : "")>Paris</option>
    <option @((string)Session["location"] == "China" ? "selected" : "")>China</option>
    <option @((string)Session["location"]== "GB" ? "selected":"")>GB</option>
</select>

You should use SelectList and MVC's HTML Helpers to build your dropdown. It looks daunting if you're new to MVC , but it simplifies a lot of stuff for you.

In your current action method that loads this page:

ViewBag.DeliveryLocation = new SelectList(new[]
{
    new { Text = "Aundh", Value = "Aundh" },
    new { Text = "Balewadi", Value = "Balewadi" },
    new { Text = "Pimpri", Value = "Pimpri" }
}, "Value", "Text", Session["Location"]);

There are a bunch of overloads for the SelectList class. So the above code creates a SelectList with the collection where Value property will be bound to option's value attribute and Text will be displayed as the option 's text. The next parameter is selectedValue . In your case, you'd want to set to whatever is there in the Session["Location"] .

You can also create a list of SelectListItem and put it in a ViewBag .

And then in your view,

@Html.DropDownList("DeliveryLocation")

That's it. So what happens here is, Razor generates a <select> element with name="DeliveryLocation" . Then it looks for a DeliveryLocation property in the ViewBag . WE have set this in our controller and the options will be set accordingly.

Let's say, you have set ViewBag.DeliveryLocationList in your controller with the SelectList . Then you'd have to tell razor where to look for a SelectList . And since ViewBag is a dynamic object, you'd have to cast it.

@Html.DropDownList("DeliveryLocation", (IEnumerable<SelectListItem>)ViewBag.DeliveryLocationList,  "Choose Your Location", new { @id= "DeliveryLocation" })

Here you have hardcoded the locations. In real applications, the option values like Locations will usually be fetched from the database. Then you don't have to make big changes if you use HTML helpers.


This link from Microsft docs has some great examples for beginners

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM