locked
Cascading DDL using non-id variable RRS feed

  • Question

  • User-1641868886 posted

    Hi,

    I have a select list on my MVC page to select a state:

    <div class="form-group">
            @Html.LabelFor(m=> m.State, new { @class = "control-label" })
            @Html.DropDownListFor((m => m.State, ViewBag.Statelist as SelectList, "- - select State - -"))
        </div>

    ...where "State" is NOT the "StateId" but rather a variable "State" that is text i.e; "CA" or "LA" etc.

    My issue is that I want to use the selected "State", say "CA" to populate a list of stores in the selected state. The "Stores" table has addresses with "State" to compare to my selected "State", but not "StateId":

    Store Address CityName State ZipCode
    Store 1001 101 Main St Los Angeles CA 90201
    Store 1005 203 First St Alameda CA 94533
    Store 3003 999 Dock Street San Francisco CA 94502

    ...and so on. So I want the selected "State", "CA" to compare to the "State" column in the stores table, not to a "StateId" which is not present in the "stores" table.

    I may also need to do a double cascade to compare a selected city name like "San Diego" to isolate stores just from that city. This would give me a third ddl, "Stores" from which I can select the desired "Store."

    My question:  Is there a way to populate ddl starting from a "State" table but using the text "CA" as the selected value, and then even choose a "CityName" from the stores table as a value to get the final stores list?

    If anyone can point me to some examples of ddl based upon text values instead of "id" values, plz reply. For all I know, this may be common. I have never done cascading, so I'm in the early learning phase. Problem is, I have a "State" table, but not a "City" table with "StateId" and "CityId." Also, no "StateId" nor "CityId" in the "Stores" table I want to read from.

    Thanks much!

    Wednesday, January 23, 2019 4:51 PM

Answers

  • User1520731567 posted

    Hi ReidMelSam,

    My issue is that I want to use the selected "State", say "CA" to populate a list of stores in the selected state. The "Stores" table has addresses with "State" to compare to my selected "State", but not "StateId":

    Actually,this is related to ViewBag.Statelist as SelectList.

    For example:

    In controller:

    ViewBag.StateList = new SelectList(datasource, "xx1", "xx2"); 

    you just exchange xx1 and xx2 positions,like:

    ViewBag.StateList = new SelectList(datasource, "xx2", "xx1"); 

    And you could refer to this link about Cascading DDL:

    https://forums.asp.net/post/6236282.aspx

    Best Regards.

    Yuki Tao

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 30, 2019 8:22 AM

All replies

  • User-474980206 posted

    the DropDownList generate a <select> with <options>. the options has an optional value and a text prop:

    <select>
      <option>text1</option>
      <option>text2</option>
    </select>
    

    in this case the as the value is not specified, it defaults to the text. you can add a value attribute

    <select>
      <option value="text1">text1</option>
      <option value="text2">text2</option>
    </select>

    if you don't want a value, its just

      var list = new SelectList(new string[] {"CA","OR","WA"});

    Wednesday, January 23, 2019 11:35 PM
  • User-1641868886 posted

    Bruce,

    Thanks. I'm such a code dummy, I can't easily translate your static list example to my MVC model/table problem. But it sounds like you're just telling me that I should use as the "Value" the column of "State" symbols ("CA", "OR" etc.) rather than the int "StateId" and I'll be fine?

    Thanks again for any ideas.

    Thursday, January 24, 2019 12:27 AM
  • User1520731567 posted

    Hi ReidMelSam,

    My issue is that I want to use the selected "State", say "CA" to populate a list of stores in the selected state. The "Stores" table has addresses with "State" to compare to my selected "State", but not "StateId":

    Actually,this is related to ViewBag.Statelist as SelectList.

    For example:

    In controller:

    ViewBag.StateList = new SelectList(datasource, "xx1", "xx2"); 

    you just exchange xx1 and xx2 positions,like:

    ViewBag.StateList = new SelectList(datasource, "xx2", "xx1"); 

    And you could refer to this link about Cascading DDL:

    https://forums.asp.net/post/6236282.aspx

    Best Regards.

    Yuki Tao

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 30, 2019 8:22 AM
  • User-1641868886 posted

    Thank you for the info. I was hung up on the idea of not using an int column like an "Id" as a "WHERE" parameter. But using the "State" symbol column, a string (like "AL", "CA" or "NY") as the parameter was just as good.

    Thanks again!

    Tuesday, February 5, 2019 10:38 PM