1

I have the following code in my view

 <td>@Html.DropDownListFor(e => e[0].Reason, new List<SelectListItem>
  (
   new[]
    {
       new SelectListItem { Text = " -Select- ", Value = "-Select-" },
       new SelectListItem { Text = " Price ", Value = "Price" },
       new SelectListItem { Text = " 3P ", Value = "3P" },
       new SelectListItem { Text = " Freight Collect ", Value = "Freight Collect" },
       new SelectListItem { Text = " Billing ", Value = "Billing" },
       new SelectListItem { Text = " Business Closure ", Value = "Business Closure" },
       new SelectListItem { Text = " Customer Service ", Value = "Customer Service" },
       new SelectListItem { Text = " Quality ", Value = "Quality" },
       new SelectListItem { Text = " Service ", Value = "Service" },
       new SelectListItem { Text = " Business Relocate ", Value = "Business Relocate" },
       new SelectListItem { Text = " Change in Relationships ", Value = "Change in Relationships" },
       new SelectListItem { Text = " Different Account Code ", Value = "Different Account Code" },
       new SelectListItem { Text = " Economic Downturn ", Value = "Economic Downturn" },
       new SelectListItem { Text = " BIC ", Value = "BIC" },
       new SelectListItem { Text = " Credit/Collections ", Value = "Credit/Collections" }

        }
        ))</td>

Every time when the page loads selected value gets changes

Can you please tell me how to set the selected value.

Thanks in Advance.

Nithesh Narayanan
  • 11,481
  • 34
  • 98
  • 138
veena
  • 103
  • 2
  • 3
  • 8
  • veena was going to add as an answer, but will just make as comment. In my opinion, you are doing this the hard way. you should really be using a viewModel that contains an `IEnumerable()`. this viewModel would then contain a list similar to your question but would have the logic contained at source to determine which SelectListItem was selected. Therefore, inside your view, you would only require to add a single line along the lines `@Html.DropDownListFor(m=>m.Reason, Model.reasonlist)`. your mileage may vary, but this would be my refactor – jim tollan Jul 30 '13 at 08:12

5 Answers5

5

Add your list to ViewBag as type IEnumerable-SelectListItem. Then use this part of code.

@Html.DropDownListFor(e => e[0].Reason, new SelectList(ViewBag.YourSelectList, "Value", "Text","YourSelectedValue")
Abhilab Das
  • 113
  • 2
  • 13
0

You can pass the selected value with model object. From the action method you can return the model object as follows

model obj=new model();
obj.Reason="Price";
return view(obj)

In the view the item with value "Price" get selected while loading itself

Or you can set the selected value using jquery

$(document).ready(function(){
   $("#Reason").val("Price");
});
Nithesh Narayanan
  • 11,481
  • 34
  • 98
  • 138
0

If you want set selected value in view:

@Html.DropDownListFor( e => e[0].Reason, new SelectList( new List<SelectListItem>(){
   new SelectListItem { Text = " -Select- ", Value = "-Select-" },
   new SelectListItem { Text = " Price ", Value = "Price" },
   new SelectListItem { Text = " 3P ", Value = "3P" },
   new SelectListItem { Text = " Freight Collect ", Value = "Freight Collect" },
   new SelectListItem { Text = " Billing ", Value = "Billing" },
   new SelectListItem { Text = " Business Closure ", Value = "Business Closure" },
   new SelectListItem { Text = " Customer Service ", Value = "Customer Service" },
   new SelectListItem { Text = " Quality ", Value = "Quality" },
   new SelectListItem { Text = " Service ", Value = "Service" },
   new SelectListItem { Text = " Business Relocate ", Value = "Business Relocate" },
   new SelectListItem { Text = " Change in Relationships ", Value = "Change in Relationships" },
   new SelectListItem { Text = " Different Account Code ", Value = "Different Account Code" },
   new SelectListItem { Text = " Economic Downturn ", Value = "Economic Downturn" },
   new SelectListItem { Text = " BIC ", Value = "BIC" },
   new SelectListItem { Text = " Credit/Collections ", Value = "Credit/Collections" }
    } , "Value", "Text", e[0].Reason ) )

For set selected value in controller, check answer: https://stackoverflow.com/a/17184481/1643075

Community
  • 1
  • 1
Xordal
  • 1,369
  • 13
  • 29
0

ok, veena,

I left a comment suggesting how I would do it if it were me. However, you are looking for an answer based on your current implementation, so here's my take on that for now:

<td>
    @{ var reasonList = new List<SelectListItem>(new[] {
        new SelectListItem { 
            Text=" -Select-",
            Value="-Select- "
        },
        new SelectListItem { 
            Selected = Model[0].Reason.Equals("Price"), 
            Text=" Price",
            Value="Price"
        },
        new SelectListItem {
            Selected = Model[0].Reason.Equals("3P"),
            Text=" 3P",
            Value="3P"
        }
        // etc etc...
      });
    }
    @Html.DropDownListFor(m => m[0].Reason, reasonList)
</td>

However, I'd strongly suggest that you move this logic up into your controller/service code as the view really becomes littered when this kind of code is included. I'd also strongly question the rationale for examining the index elements in the way that you do. I'm presuming that you have ommitted indexes [1], [2] etc for brevity. If this is the case, then you should really use a foreach loop and go thro the elements sequentially in your code. Below is a very quick example of that too (the syntax may be slightly incorrect as i'm sending via ipad, so is untested):

@foreach(var item in Model) {
   @: <td>
   var reasonList = new List<SelectListItem>(new[] {
        new SelectListItem { 
            Text=" -Select-",
            Value="-Select- "
        },
        new SelectListItem { 
            Selected = item.Reason.Equals("Price"), 
            Text=" Price",
            Value="Price"
        },
        new SelectListItem {
            Selected = item.Reason.Equals("3P"),
            Text=" 3P",
            Value="3P"
        }
        // etc etc...
      });
    @Html.DropDownListFor(m => item.Reason, reasonList)
    @: </td>
}
jim tollan
  • 22,305
  • 4
  • 49
  • 63
0

The solution is to put the selected value into the forth parameter of the SelectList object.

List<string> TheValuesYouWantToPresent = new List<string> { "one", "two", "three" };
string SelectedValue = "two";
List<SelectListItem> TheSelectListItems = TheValuesYouWantToPresent.Select(x => new SelectListItem { Text = x, Value = x}).ToList();
ViewBag.MyDropDownListDataWithASelectedItem = new SelectList(TheSelectListItems, "Text", "Value", SelectedValue);

Then in your MVC view, you do this:

@Html.DropDownListFor(model => model.DataBaseFieldToPopulate, ViewBag.MyDropDownListDataWithASelectedItem, "Invitation to select something from the list", new { @class = "a_CSS_class_name_of_your_choosing" })
netfed
  • 602
  • 8
  • 18