JimB_ Ответов: 1

Как мне автоматически выбрать значение в раскрывающемся списке на основе значения, отображаемого в textboxfor C# MVC


Hi All C# MVC programmers 

Could you please help me

How do I auto select selected Item value in dropdownlistfor(),
base on the value displayed by @html.TextboxFor()

I display values in dropdownlist implementing listC 


listC

  public void ListCDropDownList()
        {

            var itemsQuery = from k in db.listCs select k;

            ViewBag.ItemsList = itemsQuery.ToList();
        }


+-------+---------+---------+
|   id  |   Code  | ItemName|
+-------+---------+---------+
|   1   | 3015AZ  | sugar   |
+-------+---------+---------+ this is listC
|   2   | 301502  | salt    |
+-------+---------+---------+
|   3   | 509801  | tuna    |
+-------+---------+---------+


//TextboxFor displays for each item in table listA

+-------+---------+---------+
|   id  |TypeCode | MenuItem|
+-------+---------+---------+
|  1    |3015AZ-1 | Icecream|
+-------+---------+---------+ this is listA
|   2   |3015AZ-1 | Juice   |
+-------+---------+---------+
|   3   |3015AZ-1 | Cake    |
+-------+---------+---------+


 <td>
 @Html.TextBoxFor(modelItem => item.Code , new { @readonly = "readonly", @class = "form-control " })
  </td>

<td>
@Html.TextBoxFor(modelItem => item.MenuItem, new { @readonly = "readonly", @class = "form-control" })
 </td>

<td>
 @Html.DropDownListFor("listC", new SelectList(ViewBag.ItemsList, "ItemName", "ItemName"), "Please Select", new { @class = "form-control" })


Что я уже пробовал:

//HERE TODO
// HOW do I make an auto select value in Dropdowlist --
//When page loads, the dropdownlist will load the auto-select value base on the value displayed by Texboxfor

//sample: sugar; because this value is base on the value in texboxfor = Code 


 
 
 //Load listC in viewbag 

  public void ListCDropDownList()
 {

            var itemsQuery = from k in db.listCs select k;

            ViewBag.ItemsList = itemsQuery.ToList();
 }

//display listA
 public ActionResult Index()
{        
    ListCDropDownList());
    var menuList = db.listAs.ToList();

    return View(menuList .OrderBy(x => x.MenuItem).ToList());
}

 @model IEnumerable<App.Models.MYMOdel>

@foreach (var item in Model)
{  
  <td>

   @Html.TextBoxFor(modelItem => item.Code , new { @readonly = "readonly", @class 
   = "form-control " })
  </td>

   <td>
  @Html.TextBoxFor(modelItem => item.MenuItem, new { @readonly = "readonly", @class = 
   "form-control" })
   </td>

   <td>
    @Html.DropDownListFor("listC", new SelectList(ViewBag.ItemsList, "ItemName", 
   "ItemName"), "Please Select", new { @class = "form-control" })

   </td>

}

thank you everyone

1 Ответов

Рейтинг:
9

Vincent Maverick Durano

На самом деле я еще не проверял это, но вы могли бы попробовать что-то вроде этого:

public void ListCDropDownList(string code)
{
            var itemsQuery = from k in db.listCs where k.code == code select k ;
            ViewBag.ItemsList = itemsQuery.ToList();
}

public ActionResult Index(string code)
{        
    ListCDropDownList(code);
    var menuList = db.listAs.ToList();

    return View(menuList.OrderBy(x => x.MenuItem).ToList());
}


Затем вы можете изменить свой DropDownListFor заявление об этом:

@Html.DropDownListFor(item => item.Code, new SelectList(ViewBag.ItemsList, "ItemName", 
   "ItemName"), "Please Select", new { @class = "form-control" })