简体   繁体   中英

ASP.NET dropdownlist selected item value gives same ID

I am trying to develop a web application which I Need to save firm name username and password.

When I try to add firm username and password it gives me same firm ID in the database as;

Firmname : YAHOO INC
UserName : asd
Password : pass

İn formload all firms come but when ı try to save firmname, username and pass it gives me same firmname for all user save.

Any advice how I can resolve such issue?

Here is a simple example of how to create a drop down list in ASP.NET MVC using Html.DropDownListFor.

You can do it like this all inlined in your *.cshtml file like so:

@Html.DropDownListFor(model => model.Package.State, new SelectList(
                  new List<Object>{ 
                       new { value = 0 , text = "Red"  },
                       new { value = 1 , text = "Blue" },
                       new { value = 2 , text = "Green"}
                    },
                  "value",
                  "text",
                   2))

With produces this:

<select id="Package_State" name="Package.State"><option value="0">Red</option>
<option value="1">Blue</option>
<option value="2">Green</option>
</select>

The model => model.Package.State expression is used to generate the id and name on the select.

The string value and text matter as they need to match the model attributes of the listItem in the list.

Or you can create your own List collection (with different fields for value and text) in your controller, set it on you model, and then use in your view like this:

@Html.DropDownListFor(model => model.Package.State.Id, new SelectList(
              Model.PackageStates,
              "id",
              "name",
              Model.Package.State.Id))

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