<td>@Html.DropDownListFor(model => model.CITY_ID, new SelectList(Model.CITY, "City_ID", "City_English_Name"), "Please select a city")</td>
I need to allow the user to enter a new value which is not in the Model.CITY list, are there any dropdownlist helper to make it easy to configure ? I do not think I need to do any changes on server side.
Edit: just in case need to update server codes, here is the code on viewModel:
CITY = db.tbl_CITY.OrderBy(t => t.CITY_ENGLISH_NAME).ToList(), so I guess I need to add a empty entry in this list, right? There is no blank row entry in the tbl_CITY table
I need to allow the user to enter a new value which is not in the Model.CITY list, are there any dropdownlist helper to make it easy to configure ? I do not think I need to do any changes on server side.
It sounds like what you are looking for a "combobox", which is basically a drop down list that supports fields the user can freely type into, which isn't supported using any of the HTML Helpers that come out of the box with ASP.NET MVC. If it's an option,
there are quite a few available from many of the popular third-party component vendors out there, or you could likely just use a purely-Javascript based one.
Another option would be to consider including an option that would simply say "Other" that would cause another textbox to appear to allow the user to freely enter a value. Then within your server side code, just add a bit of logic to determine if there was
one that was freely entered and use that:
// Psuedocode to determine which value to use on the server-side (use other if it has a value)
var city = String.IsNullOrEmpty(otherValue) ? dropDownValue : otherValue;
I need to allow the user to enter a new value which is not in the Model.CITY list, are there any dropdownlist helper to make it easy to configure ? I do not think I need to do any changes on server side.
It sounds like what you are looking for a "combobox", which is basically a drop down list that supports fields the user can freely type into, which isn't supported using any of the HTML Helpers that come out of the box with ASP.NET MVC. If it's an option,
there are quite a few available from many of the popular third-party component vendors out there, or you could likely just use a purely-Javascript based one.
Another option would be to consider including an option that would simply say "Other" that would cause another textbox to appear to allow the user to freely enter a value. Then within your server side code, just add a bit of logic to determine if there was
one that was freely entered and use that:
// Psuedocode to determine which value to use on the server-side (use other if it has a value)
var city = String.IsNullOrEmpty(otherValue) ? dropDownValue : otherValue;
Hi Rion, Thank you so much for your quick response, I did not realize it is so hard to make it available, I thought there should be an option in @Html.DropdownListfor helper class to do it, now it seems it is not possible.
Another option, since the data on server side is like this:
CITY = db.tbl_CITY.OrderBy(t => t.CITY_ENGLISH_NAME).ToList(),
can I add an blank entry index is zero like this: CITY = db.tbl_CITY.OrderBy(t => t.CITY_ENGLISH_NAME).ToList().Insert(0, "",""), but this does not work.
CITY = db.tbl_CITY.OrderBy(t => t.CITY_ENGLISH_NAME).ToList().Insert(0, "",""), but this does not work.
The insert method has no return type, so this line of code will get an error, you can change it like this:
CITY = db.tbl_CITY.OrderBy(t => t.CITY_ENGLISH_NAME).ToList();
CITY.Insert(0, new City());
Best Regards,
Jiadong Meng
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
CITY = db.tbl_CITY.OrderBy(t => t.CITY_ENGLISH_NAME).ToList().Insert(0, "",""), but this does not work.
The insert method has no return type, so this line of code will get an error, you can change it like this:
CITY = db.tbl_CITY.OrderBy(t => t.CITY_ENGLISH_NAME).ToList();
CITY.Insert(0, new City());
Best Regards,
Jiadong Meng
Hi Jiadong, thanks a lot for your help...
I will try your solution when I finish my vacation and back to Canada on Feb 17, 2020 and will let you know then.
but before I try it, I am not sure if the "new City()" used here will work, since the SQL table name called tbl_CITY, do I need to define the new object City()?
if you read my entire thread, you will see the bottom line here is to allow the new entry list in a @Html.DropDownListFor field, basically, user have the option to enter a new entry.
Member
366 Points
2214 Posts
How to make the @Html.DropDownListFor to allow enter a new value?
Jan 15, 2020 03:01 PM|Peter Cong|LINK
In my mvc razor view, I have the following codes:
I need to allow the user to enter a new value which is not in the Model.CITY list, are there any dropdownlist helper to make it easy to configure ? I do not think I need to do any changes on server side.
Edit: just in case need to update server codes, here is the code on viewModel:
CITY = db.tbl_CITY.OrderBy(t => t.CITY_ENGLISH_NAME).ToList(), so I guess I need to add a empty entry in this list, right? There is no blank row entry in the tbl_CITY table
Thanks
All-Star
114593 Points
18503 Posts
MVP
Re: How to make the @Html.DropDownListFor to allow enter a new value?
Jan 15, 2020 03:50 PM|Rion Williams|LINK
It sounds like what you are looking for a "combobox", which is basically a drop down list that supports fields the user can freely type into, which isn't supported using any of the HTML Helpers that come out of the box with ASP.NET MVC. If it's an option, there are quite a few available from many of the popular third-party component vendors out there, or you could likely just use a purely-Javascript based one.
Another option would be to consider including an option that would simply say "Other" that would cause another textbox to appear to allow the user to freely enter a value. Then within your server side code, just add a bit of logic to determine if there was one that was freely entered and use that:
Member
366 Points
2214 Posts
Re: How to make the @Html.DropDownListFor to allow enter a new value?
Jan 15, 2020 04:29 PM|Peter Cong|LINK
Hi Rion, Thank you so much for your quick response, I did not realize it is so hard to make it available, I thought there should be an option in @Html.DropdownListfor helper class to do it, now it seems it is not possible.
Another option, since the data on server side is like this:
CITY = db.tbl_CITY.OrderBy(t => t.CITY_ENGLISH_NAME).ToList(),
can I add an blank entry index is zero like this: CITY = db.tbl_CITY.OrderBy(t => t.CITY_ENGLISH_NAME).ToList().Insert(0, "",""), but this does not work.
Member
366 Points
2214 Posts
Re: How to make the @Html.DropDownListFor to allow enter a new value?
Jan 16, 2020 02:11 AM|Peter Cong|LINK
https://forums.asp.net/t/2080099.aspx?Add+dropdown+list+and+allow+adding+new+values
https://codereview.stackexchange.com/questions/175103/handing-others-option-in-dropdown-in-asp-net-mvc-5
are there any better options? I prefer to enter the new value directly in the dropdownlistfor field, not creating an extra field as these example,
thanks
Participant
1320 Points
491 Posts
Re: How to make the @Html.DropDownListFor to allow enter a new value?
Jan 16, 2020 06:43 AM|jiadongm|LINK
Hi Peter,
The insert method has no return type, so this line of code will get an error, you can change it like this:
Best Regards,
Jiadong Meng
Member
366 Points
2214 Posts
Re: How to make the @Html.DropDownListFor to allow enter a new value?
Jan 21, 2020 09:29 AM|Peter Cong|LINK
Hi Jiadong, thanks a lot for your help...
I will try your solution when I finish my vacation and back to Canada on Feb 17, 2020 and will let you know then.
but before I try it, I am not sure if the "new City()" used here will work, since the SQL table name called tbl_CITY, do I need to define the new object City()?
if you read my entire thread, you will see the bottom line here is to allow the new entry list in a @Html.DropDownListFor field, basically, user have the option to enter a new entry.
thanks,