Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
@Html.DropDownListFor(Function(model) model.StateId, New SelectList(ViewData("StateList"), "Id", "Name"), "--Select--")


Need to set Id value to write onchange function

What I have tried:

pre> @Html.DropDownListFor(Function(model) model.StateId, New SelectList(ViewData("StateList"), "Id", "Name"), "--Select--", New{@Id="Country")
Posted
Updated 7-Feb-19 0:28am
Comments
CHill60 4-Feb-19 9:20am    
And what happens when you try to set that value? What error do you get?
Chinnu2020 4-Feb-19 9:35am    
Type or with expected
)' expected.
F-ES Sitecore 4-Feb-19 10:12am    
Look at your code;

@Html.DropDownListFor(Function(model) model.StateId, New SelectList(ViewData("StateList"), "Id", "Name"), "--Select--", New{@Id="Country")

After "New" you open a curly bracket but don't close it

@Html.DropDownListFor(Function(model) model.StateId, New SelectList(ViewData("StateList"), "Id", "Name"), "--Select--", New{@Id="Country"})
CHill60 4-Feb-19 10:33am    
Worth posting as the solution
Chinnu2020 4-Feb-19 10:31am    
Have given the same but still it is giving error

1 solution

Since you're using VB.NET, you need to use VB.NET syntax:
VB.NET
@Html.DropDownListFor(Function(model) model.StateId, New SelectList(ViewData("StateList"), "Id", "Name"), "--Select--", New With { .Id = "Country" })

Anonymous Types (Visual Basic) | Microsoft Docs[^]
 
Share this answer
 
Comments
Chinnu2020 6-Feb-19 10:11am    
Thanks for that.

can you please guide me how to load data based on state data based on country ID.

I have done but while editing the record value is not loading as i given below code though model is holding the data.
@Html.DropDownListFor(Function(model) model.StateId, New SelectList("", "Id", "Name"), "--Select--")
@Html.ValidationMessageFor(Function(model) model.StateId)
Chinnu2020 6-Feb-19 10:11am    
Iam loading based on change event
Chinnu2020 6-Feb-19 10:31am    
$(document).ready(function () {


debugger;
$("#CountryId").change(function () {
var CountryId = parseInt($(this).val());

if (!isNaN(CountryId)) {
var ddlState = $('#StateId');
ddlState.empty();
ddlState.append($("").val('').html('Please wait ...'));
$.ajax({
url: '/AEContact/GetStates?ID=' + CountryId,
type: 'GET',
dataType: 'json',
cache: false,
data: { CountryId: CountryId },
success: function (d) {
ddlState.empty(); // Clear the please wait
ddlState.append($("").val('').html('Select State'));
$.each(d, function (i, states) {
ddlState.append($("").val(states.Id).html(states.Name));
});
},
error: function () {
alert('Error!');
}
});
}
});
});
Chinnu2020 6-Feb-19 10:32am    
Country data and state date is loading in Model , change event is not calling while editing
Richard Deeming 6-Feb-19 10:46am    
You have already posted this as a separate question:
https://www.codeproject.com/Questions/1276085/Load-dropdown-based-on-other-dropdown[^]

I suggest you edit that question (using the green "Improve question" link) and add this additional information to it.

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