Click here to Skip to main content
15,889,116 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
C#
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    |
+-------+---------+---------+


HTML
 <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" })


What I have tried:

C#
//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());
}

HTML
 @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
Posted
Updated 10-Apr-19 10:29am

1 solution

Haven't really tested this out, but you could try something like this:

C#
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());
}


Then you can modify your DropDownListFor declaration to this:

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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900