如何保存选定的下拉列表值? [英] How to Save selected dropdownlist value?
问题描述
您好,我知道如何获取所选值,然后将其保存在数据库中。
我有一个国家/地区实体: countryId
, country_Name
我还有一个拥有此属性的个人资料实体:
< pre lang =c#> public selectlist countrylist { get ; set ;}
然后在控制器的创建动作中,我用linq从数据库中获取国家/地区列表
在我看来我实现了这样的下拉列表:
@ html.dropdownlist(ddlcountry,model.countrylist)
但是如果我提交它,我会得到 null
<$的值错误c $ c> contrylist 。
请问,如何将其保存在数据库中。在配置文件表中,我现在看到 countrylist_datavalue
& countrylist_datatext
嘿,您需要再添加一个属性来绑定表单中的结果。
喜欢,public int CountryId {get;组; }
以下是使用此属性绑定值的代码中的一点变化。
@ Html.DropdownList(Model.CountryId, Model.CountryList)
现在,在提交时,您将获得Model.CountryId中的选定值。您可以直接将此选定值保存到数据库中。
此外,您可以在CountryId属性中设置默认值。
希望它有效!! !
Hello, i dnt knw how to get the selected value and then save it in database.
I have a country entity: countryId
, country_Name
and i also have a profile entity with this property:
public selectlist countrylist {get; set;}
then in the create action of the controller, i used linq to get the list of countries from database
and in my view i implemented the dropdownlist like this:
@html.dropdownlist("ddlcountry", model.countrylist)
but if i submit it, i get null
value error for contrylist
.
Please, how can i save it in database. In profile table, i now see countrylist_datavalue
& countrylist_datatext
Hey, you need to add one more property to bind your result in the form.
Like, public int CountryId { get; set; }
Here is a little change in your code to bind the value with this property.
@Html.DropdownList(Model.CountryId, Model.CountryList)
Now, on submitting you will get the selected value in Model.CountryId. And you can directly save this selected value into the database.
Also, you can set by default value in the CountryId property.
Hope it works !!!
这篇关于如何保存选定的下拉列表值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!