@Html.DropDownListFor(model => model.IdRaisedBy, Model.FieldEmployees, "Select Field Employee", new { @id = "EmployeesDropDown", @class = "form-control", @style = "width: 100%;" })
My issue occurs when opening the form (containing the above helper) as an edit or delete function. For some reason the IdRaisedBy value that is passed to the view, doesn't show up as selected within the select input.
I've done some debugging and can confirm that the value is passed to the view with no issues, and that value is also contained within the select inputs options. Another weird aspect of this problem I noticed was that the option within the select list that
matches the IdRaisedBy value is marked with the Selected html attribute , however, when the form is submitted it takes the value at the top of the list (which is also being displayed as selected within the drop down, despite not being marked as such).
I'm stumped as to why this is happening so thanks in advance for any advice.
most likely you have some client script that is effecting the selected item in the select. use the browsers debugging tools, to check the post back data, and the value of the select during submit.
According to your description, is your problem not successfully displaying the default checked value in the dropdownlist?
JackBodman
Another weird aspect of this problem I noticed was that the option within the select list that matches the IdRaisedBy value is marked with the Selected html attribute , however, when the form is submitted it takes the value at the top of the list (which is
also being displayed as selected within the drop down, despite not being marked as such).
For this problem, we need you to provide more detailed code for testing.
I created a simple case that you can refer to:
public class School
{
public int id { get; set; }
public string school_name { get; set; }
}
public class CategoryModel
{
public int IdRaisedBy { get; set; }
public IEnumerable<SelectListItem> FieldEmployees { get; set; }
}
public ActionResult Index()
{
var db = new MyContext();
CategoryModel categoryModel =
new CategoryModel()
{
IdRaisedBy = 3,
FieldEmployees = new SelectList(db.School, "id", "school_name")
};
}
have some client script that is effecting the selected item in the select
Bruce was right, I had completely forgotten about some jQuery I had written to default select the logged in user within the Dropdown. I have now amended this so that it only runs during a create action, meaning on edit and delete it will show the selected
value.
Member
3 Points
7 Posts
Drop Down List Helper, Value marked as Selected In Edit Function, Not Actually Selected
Feb 05, 2020 03:04 PM|JackBodman|LINK
Have a bit of a weird one,
I have this drop down list helper function.
My issue occurs when opening the form (containing the above helper) as an edit or delete function. For some reason the IdRaisedBy value that is passed to the view, doesn't show up as selected within the select input.
I've done some debugging and can confirm that the value is passed to the view with no issues, and that value is also contained within the select inputs options. Another weird aspect of this problem I noticed was that the option within the select list that matches the IdRaisedBy value is marked with the Selected html attribute , however, when the form is submitted it takes the value at the top of the list (which is also being displayed as selected within the drop down, despite not being marked as such).
I'm stumped as to why this is happening so thanks in advance for any advice.
All-Star
58134 Points
15641 Posts
Re: Drop Down List Helper, Value marked as Selected In Edit Function, Not Actually Selected
Feb 05, 2020 04:37 PM|bruce (sqlwork.com)|LINK
most likely you have some client script that is effecting the selected item in the select. use the browsers debugging tools, to check the post back data, and the value of the select during submit.
Contributor
3710 Points
1043 Posts
Re: Drop Down List Helper, Value marked as Selected In Edit Function, Not Actually Selected
Feb 06, 2020 08:09 AM|Yongqing Yu|LINK
Hi JackBodman,
According to your description, is your problem not successfully displaying the default checked value in the dropdownlist?
For this problem, we need you to provide more detailed code for testing.
I created a simple case that you can refer to:
public ActionResult Index() { var db = new MyContext(); CategoryModel categoryModel = new CategoryModel() { IdRaisedBy = 3, FieldEmployees = new SelectList(db.School, "id", "school_name") }; }
Here is the result :
You can also refer to this link:
Set Default Value To Dropdown List From Database In ASP.NET MVC
Best Regards,
YongQing.
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
3 Points
7 Posts
Re: Drop Down List Helper, Value marked as Selected In Edit Function, Not Actually Selected
Feb 06, 2020 10:00 AM|JackBodman|LINK
Bruce was right, I had completely forgotten about some jQuery I had written to default select the logged in user within the Dropdown. I have now amended this so that it only runs during a create action, meaning on edit and delete it will show the selected value.
Thanks Bruce.