如何从下拉列表中选择字符串 [英] how to get the string from dropdown list

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

问题描述

我有这样的code在我的控制器索引视图。

I have this code in my controller for Index view..

 public ActionResult Index(int? id)
        {
            _viewModel.ServiceTypeListAll = new SelectList(_bvRepository.GetAllServiceTypes().ToList().OrderBy(n => n.ServiceTypeName).ToList(), "ServiceTypeId", "ServiceTypeName");
            return View(_viewModel);
        }

使用这个我能dispaly所有ServiceTypes在我的DropDownList箱视图。在code是

Using this I am able to dispaly all the ServiceTypes in my view in dropdownlist box. the code is

<%=Html.DropDownList("ServiceTypeListAll", new SelectList(Model.ServiceTypeListAll,"Value","Text"))%>

当我试图从查看选定DROPDOWNLIST值到控制器我acceesing这样的..

When I am trying to get the Selected Dropdownlist value from View to controller I am acceesing like this..

string categoryName = collection["ServiceTypeListAll"]; // collectoin refers FormCollection

我期待类别名称应该是字符串什么都喜欢我DROPDOWNLIST框中显示我。

I am expecting CategoryName should be string like what ever I am showing in Dropdownlist box.

我得到的整数值?

是somethign我做错了。

is that somethign I am doing wrong

感谢

推荐答案

DropDownList的值是它的ID属性,你指定为 ServiceTypeId

你需要指定的ID为 ServiceTypeName 来代替,就像这样:

The value of a dropdownlist is its ID property, which you're specifying as ServiceTypeId.
You need to specify the ID as ServiceTypeName instead, like this:

_viewModel.ServiceTypeListAll = new SelectList(_bvRepository.GetAllServiceTypes().OrderBy(n => n.ServiceTypeName), "ServiceTypeName", "ServiceTypeName");

此外, Model.ServiceTypeListAll 已经是的SelectList ;你不需要把它包起来:

Also, Model.ServiceTypeListAll is already a SelectList; you don't need to wrap it:

<%=Html.DropDownList("ServiceTypeListAll", Model.ServiceTypeListAll)%>

这篇关于如何从下拉列表中选择字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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