如何从下拉列表中获取所选值? [英] How to get the selected value from dropdownlist?

查看:83
本文介绍了如何从下拉列表中获取所选值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我知道如何获取所选值,然后将其保存在数据库中。

我有一个国家/地区实体:countryId,country_Name



我也有一个有这个属性的个人资料实体:

公共选择列表countrylist {get;设置;}



然后在控制器的创建动作中,我用linq从数据库中获取国家列表



并且在我的视图中我实现了这样的下拉列表:

@html.dropdownlist(ddlcountry,model.countrylist)但是如果我提交它,我会获得contrylist的null值错误。



请问,如何将其保存在数据库中。在配置文件表中,我现在看到countrylist_datavalue&countrylist_datatext

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

推荐答案

嘿,您需要再添加一个属性来绑定表单中的结果。



喜欢,public int CountryId {get;组; }



以下是使用此属性绑定值的代码中的一点变化。

@ Html.DropdownList(Model.CountryId, Model.CountryList)



现在,在提交时,您将获得Model.CountryId中的选定值。并且您可以直接将所选值保存到数据库中。



此外,您可以在CountryId属性中设置默认值。



希望它有效!!!
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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆