I've got the dependent drop-down feature working on the Create Record page but I'm stumped at the Update page how do I set the value of the drop-downs to what is in the database.
var countylist = db.Query("SELECT * FROM tbCountyList WHERE tbCountyList.StateAbbrev = @0", firm.FirmState);
var statelist = db.Query("SELECT StateName, StateAbbrev FROM tbStateList");
List<SelectListItem> items = new List<SelectListItem>();
foreach (var item in statelist)
{
items.Add(new SelectListItem { Text = item.StateName, Value = item.StateAbbrev });
}
<tr>
<td>State of marriage:</td>
<td>
@Html.DropDownList("StateMarriage", items)
</td>
</tr>
<tr>
<td>County of marriage:</td>
<td>
<select id="CountyMarriage" name="CountyMarriage">
<option>Select State of marriage first...</option>
</select>
</td>
</tr>
You know the County that was selected previously, so you can also establish which state the county is in. You populate the State dropdownlist with all states, and ensure that the one that the chosen county is in is selected:
var items = statelist.Select(s => new SelectListItem {
Value = s.StateAbbrev,
Text = s.StateName,
Selected = s.StateAbbrev == <selected state> ? true : false
});
Equally, you prepopulate the county dropdown with the selected value in the same way. If the user wants to change the selection, your jquery will do its thing of the user wants to choose from another state.
Okay, so I've got the State drop-down list defaulting to what is in the database by adding your code:
@{
var db = Database.Open("myDB");
var userfilter = WebSecurity.CurrentUserId;
var user = db.QuerySingle("SELECT * FROM UserProfile WHERE UserId = @0", WebSecurity.CurrentUserId);
var firm = db.QuerySingle("SELECT * FROM tbFirmInfo INNER JOIN UserProfile ON tbFirmInfo.FirmKey = UserProfile.FirmKey WHERE UserID = @0", WebSecurity.CurrentUserId);
var getGeneralInformation = db.QuerySingle("SELECT * FROM tbGeneralInformation WHERE UserId = @0", WebSecurity.CurrentUserId);
var countylist = db.Query("SELECT * FROM tbCountyList WHERE tbCountyList.StateAbbrev = @0", firm.FirmState);
var statelist = db.Query("SELECT StateName, StateAbbrev FROM tbStateList");
var items = statelist.Select(s => new SelectListItem { Text = s.StateName, Value = s.StateAbbrev, Selected = s.StateAbbrev == getGeneralInformation.StateMarriage ? true : false });
Now I'm a bit confused on the CountyList because the JSON is calling it from a seperate file with the following code:
@{
var db = Database.Open("myDB");
var source = db.Query("SELECT CountyName FROM tbCountyList WHERE StateAbbrev = @0", Request["StateMarriage"]);
Json.Write(source, Response.Output);
}
Presumably, the JSON call is only made when the change event fires on the state dropdownlist, so when the page is first loaded, you can populate the county dropdown with countylist and a preselected value:
var countyItems = countylist.Select(c => new SelectListItem { Text = c.CountyName, Value = c.CountyAbbrev, Selected = c.CountyAbbrev == some_value ? true : false });
jmbishop
Member
99 Points
34 Posts
Setting values in jQuery cascading drop-down on Update page.
May 04, 2012 06:11 AM|LINK
I've got the dependent drop-down feature working on the Create Record page but I'm stumped at the Update page how do I set the value of the drop-downs to what is in the database.
var countylist = db.Query("SELECT * FROM tbCountyList WHERE tbCountyList.StateAbbrev = @0", firm.FirmState); var statelist = db.Query("SELECT StateName, StateAbbrev FROM tbStateList"); List<SelectListItem> items = new List<SelectListItem>(); foreach (var item in statelist) { items.Add(new SelectListItem { Text = item.StateName, Value = item.StateAbbrev }); }<script type="text/javascript"> $(function () { $("#StateMarriage").change(function () { var selectedState = $(this).val(); $.getJSON('CountyList.cshtml', { StateMarriage: selectedState }, function (countylist) { var countySelect = $('#CountyMarriage'); countySelect.empty(); $.each(countylist, function (index, county) { countySelect.append( $('<option/>') .attr('value', county.CountyName) .text(county.CountyName) ); }); }); }); }); </script><tr> <td>State of marriage:</td> <td> @Html.DropDownList("StateMarriage", items) </td> </tr> <tr> <td>County of marriage:</td> <td> <select id="CountyMarriage" name="CountyMarriage"> <option>Select State of marriage first...</option> </select> </td> </tr>Thanks for any help.
J Bishop
Dino He - MS...
Star
8068 Points
1023 Posts
Microsoft
Re: Setting values in jQuery cascading drop-down on Update page.
May 07, 2012 12:33 PM|LINK
HI
You can refer to this:
http://weblogs.asp.net/rajbk/archive/2010/05/20/cascadingdropdown-jquery-plugin-for-asp-net-mvc.aspx
If you have any feedback about my replies, please contact msdnmg@microsoft.com
Microsoft One Code Framework
bhaskar.mule
Contributor
2270 Points
659 Posts
Re: Setting values in jQuery cascading drop-down on Update page.
May 07, 2012 12:41 PM|LINK
hi
i hope it will help you
http://csharpektroncmssql.blogspot.com/2011/12/cascading-dropdownl-lists-in-aspnet.html
Site:Rare technical solutions
Mikesdotnett...
All-Star
154852 Points
19855 Posts
Moderator
MVP
Re: Setting values in jQuery cascading drop-down on Update page.
May 07, 2012 06:54 PM|LINK
You know the County that was selected previously, so you can also establish which state the county is in. You populate the State dropdownlist with all states, and ensure that the one that the chosen county is in is selected:
var items = statelist.Select(s => new SelectListItem { Value = s.StateAbbrev, Text = s.StateName, Selected = s.StateAbbrev == <selected state> ? true : false });Equally, you prepopulate the county dropdown with the selected value in the same way. If the user wants to change the selection, your jquery will do its thing of the user wants to choose from another state.
Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
jmbishop
Member
99 Points
34 Posts
Re: Setting values in jQuery cascading drop-down on Update page.
May 09, 2012 05:22 AM|LINK
Okay, so I've got the State drop-down list defaulting to what is in the database by adding your code:
@{ var db = Database.Open("myDB"); var userfilter = WebSecurity.CurrentUserId; var user = db.QuerySingle("SELECT * FROM UserProfile WHERE UserId = @0", WebSecurity.CurrentUserId); var firm = db.QuerySingle("SELECT * FROM tbFirmInfo INNER JOIN UserProfile ON tbFirmInfo.FirmKey = UserProfile.FirmKey WHERE UserID = @0", WebSecurity.CurrentUserId); var getGeneralInformation = db.QuerySingle("SELECT * FROM tbGeneralInformation WHERE UserId = @0", WebSecurity.CurrentUserId); var countylist = db.Query("SELECT * FROM tbCountyList WHERE tbCountyList.StateAbbrev = @0", firm.FirmState); var statelist = db.Query("SELECT StateName, StateAbbrev FROM tbStateList"); var items = statelist.Select(s => new SelectListItem { Text = s.StateName, Value = s.StateAbbrev, Selected = s.StateAbbrev == getGeneralInformation.StateMarriage ? true : false });Now I'm a bit confused on the CountyList because the JSON is calling it from a seperate file with the following code:
@{ var db = Database.Open("myDB"); var source = db.Query("SELECT CountyName FROM tbCountyList WHERE StateAbbrev = @0", Request["StateMarriage"]); Json.Write(source, Response.Output); }Thanks again.
J
Mikesdotnett...
All-Star
154852 Points
19855 Posts
Moderator
MVP
Re: Setting values in jQuery cascading drop-down on Update page.
May 09, 2012 06:24 AM|LINK
Presumably, the JSON call is only made when the change event fires on the state dropdownlist, so when the page is first loaded, you can populate the county dropdown with countylist and a preselected value:
var countyItems = countylist.Select(c => new SelectListItem { Text = c.CountyName, Value = c.CountyAbbrev, Selected = c.CountyAbbrev == some_value ? true : false });
(I made up the property names...)
Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter