-1

@Html.DropDownList - set particular text at top to display

code:

@Html.DropDownList("Plant",ViewData["PlantList"] as SelectList, new {style="height: 29px;"})

The dropdownlist has five items from viewdata. They are one, two, three, four, five.

I am having another as viewdata["displaytext"]. Here any 1 value from the options will be present. say for example now three is present in this viewdata. How to set this three to be display text

EDIT Adding controller method:

public ActionResult PlantList()
{
     ...
     ...
     ViewData["PlantList"] = pList;
     ViewData["displaytext"] = "three";
     return view();
}
Earth
  • 3,477
  • 6
  • 37
  • 78
  • Set the value of property `Plant` to `"three"` in the GET method before you pass the model to the view, and the option with `"three"` will be selected. –  Oct 13 '16 at 12:36
  • Can you give as a short answer for my understanding. – Earth Oct 13 '16 at 12:40
  • I just did in the comment :). You have not shown your model (do you even have a property named `Plant`), or the controller method, and a detailed answer is not possible until you do so. –  Oct 13 '16 at 12:42
  • I am passing `ViewData["PlantList"]` as a five options values. Then I am passing `viewdata["displaytext"]` as a single value. Then I am having the line of code as `return view();` – Earth Oct 13 '16 at 12:44
  • Edit your question with all the relevant code. –  Oct 13 '16 at 12:46
  • I had updated my question – Earth Oct 13 '16 at 12:50
  • @StackOverflow You might want to look at [Best programming practice of using DropDownList in ASP.Net MVC](http://stackoverflow.com/a/37819577/296861). – Win Oct 13 '16 at 12:52
  • Your not returning a model to the view (and you still have not even shown the model). It needs a property `string Plant` and you set the value in the controller - `var model = new MyModel{ Plant = "three" }; return View(model);` and the correct option will be displayed (that's how model binding works). And your model should also contain `IEnumerable PlantList` rather that using `ViewData`. And you should always use the strongly typed `***For()` methods - `@Html.DropDownListFor(m => m.Plant, Model.PlantList)` –  Oct 13 '16 at 12:53
  • already `cshtml` page uses another model for different functionality. How to add another model in that page. – Earth Oct 13 '16 at 12:56
  • Then create a view model ([What is ViewModel in MVC?](http://stackoverflow.com/questions/11064316/what-is-viewmodel-in-mvc)) which is the very first thing you should be doing anyway –  Oct 13 '16 at 12:59

1 Answers1

0

Create a model;

public class MyModel
{
    public int Number { get; set; }
}

Return model to view.

public ActionResult Index
{
    var model = new MyModel();
    model.Number = "three";
    return View(model);
}

and in you view;

 @Html.DropDownListFor(m=>m.Number,ViewData["PlantList"] as SelectList,"", new {style="height: 29px;"})

And the "three" option will be selected.

This is a simple example. Create the related code with your model.

Qsprec
  • 265
  • 3
  • 11