how to get the string from dropdown list

妖精的绣舞 提交于 2019-12-13 07:10:55

问题


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);
        }

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"))%>

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

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

I am getting Integer values?

is that somethign I am doing wrong

thanks


回答1:


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");

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

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



回答2:


This is what a select box looks like in HTML

<select>
  <option value="1">Item 1</option>
  <option value="2">Item 2</option>
  <option value="3">Item 3</option>
</select>

So the value you get in your controller is the selected value from the select box, and this is a number.

If you want to use the items text then there are two possibilities:

1) set the value of the options to the value of the text

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

2) after getting the int value load the appropriate object from your repository

int categoryId = Convert.ToInt32(collection["ServiceTypeListAll"]);
string categoryName = _bvRepository.Get(categoryId); // or whatever method loads your object



回答3:


Your call to Html.DropDownList() will produce html that looks like this:

<select name="ServiceTypeListAll">
  <option value="1">Service Type 1</option>
  <option value="2">Service Type 2</option>
  <option value="3">Service Type 3</option>
</select>

The value attribute is for whichever option is selected is what will appear in the FormCollection. If you really want the ServiceTypeName string instead of the ServiceTypeId, you can modify your SelectList constructor like this:

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

in order to produce html that looks like this:

<select name="ServiceTypeListAll">
  <option value="Service Type 1">Service Type 1</option>
  <option value="Service Type 2">Service Type 2</option>
  <option value="Service Type 3">Service Type 3</option>
</select>

Also, incidentally, you should be able to simplify you HtmlHelper call to this:

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

No need to create another SelectList ...



来源:https://stackoverflow.com/questions/3526814/how-to-get-the-string-from-dropdown-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!