views:

36

answers:

3

Hello friends,

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  A: 

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
Dave
I dont need to the Number I need Item1 or Item 2.. in my controller. thanks
Just updated my answer to include a solution for that
Dave
Dave thanks for your update and thanks for your time.. SLaks solution worked for me Great.. Thanks
Dave please can you help me out with this post. http://stackoverflow.com/questions/3526894/how-to-show-dropdownlist-value-on-edit-using-asp-net-mvc
+1  A: 

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)%>
SLaks
Its working perfectly what I am expecting.. thanks
SLaks.. Can you help me out one more my problem! Please.. I will post differnt Question Plese look in to that.. thanks
SLaks.. Please can you look in to this post.. http://stackoverflow.com/questions/3526894/how-to-show-dropdownlist-value-on-edit-using-asp-net-mvc
+1  A: 

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 ...

Peter
Thanks for your time peter..