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!
"Look at it go, Homer; this one's gonna go for miles!"
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.
"Look at it go, Homer; this one's gonna go for miles!"
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:
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
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!
"Look at it go, Homer; this one's gonna go for miles!"
Member
39 Points
404 Posts
Cascading DDL using non-id variable
Jan 23, 2019 04:51 PM|ReidMelSam|LINK
Hi,
I have a select list on my MVC page to select a state:
...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":
...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!
All-Star
57874 Points
15512 Posts
Re: Cascading DDL using non-id variable
Jan 23, 2019 11:35 PM|bruce (sqlwork.com)|LINK
the DropDownList generate a <select> with <options>. the options has an optional value and a text prop:
in this case the as the value is not specified, it defaults to the text. you can add a value attribute
if you don't want a value, its just
var list = new SelectList(new string[] {"CA","OR","WA"});
Member
39 Points
404 Posts
Re: Cascading DDL using non-id variable
Jan 24, 2019 12:27 AM|ReidMelSam|LINK
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.
Contributor
3710 Points
1431 Posts
Re: Cascading DDL using non-id variable
Jan 30, 2019 08:22 AM|Yuki Tao|LINK
Hi ReidMelSam,
Actually,this is related to ViewBag.Statelist as SelectList.
For example:
In controller:
you just exchange xx1 and xx2 positions,like:
And you could refer to this link about Cascading DDL:
https://forums.asp.net/post/6236282.aspx
Best Regards.
Yuki Tao
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Member
39 Points
404 Posts
Re: Cascading DDL using non-id variable
Feb 05, 2019 10:38 PM|ReidMelSam|LINK
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!