简体   繁体   English

ASP.NET下拉列表选择的项目值给出相同的ID

[英]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. 我正在尝试开发一个Web应用程序,我需要保存公司名称的用户名和密码。

When I try to add firm username and password it gives me same firm ID in the database as; 当我尝试添加公司用户名和密码时,它在数据库中为我提供了相同的公司ID;

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. 在formload中,所有公司都会出现,但是当我尝试保存公司名称,用户名并通过它时,所有用户保存都给我相同的公司名称。

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. 这是一个简单的示例,说明如何使用Html.DropDownListFor在ASP.NET MVC中创建下拉列表。

You can do it like this all inlined in your *.cshtml file like so: 您可以像这样在* .cshtml文件中内联所有代码:

@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. model => model.Package.State表达式用于在选择项上生成ID和名称。

The string value and text matter as they need to match the model attributes of the listItem in the list. 字符串值和文本很重要,因为它们需要与列表中listItem的模型属性匹配。

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: 或者,您可以在控制器中创建自己的List集合(值和文本的不同字段),在模型上进行设置,然后在视图中使用,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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