@Html.DropDownListFor(m => m.ExamDetails[i].Grade, Model.GradeItems, new { @id = "Grade_" + i })
Actually,this is a Inline problem,when the parameter is
one of a multi-line collection,the dropdownlistfor does not know
what should be selected.
tricman10
When I show files it always shows me the value 5. Not the value 6 in this example.</div>
So dropdownlistfor can only display the first one option of the GradesList(5) rather than 6.
I make a demo,you could refer to it:
Model:
public class StudentCourse_VM
{
public List<Course> CourseList { get; set; }
public IEnumerable<SelectListItem> GradeItems { get; set; }//5,6,7,8,9,10...
}
public class Course
{
public int CourseID { get; set; }
public string CourseName { get; set; }
public string CourseCode { get; set; }
}
Controller:
public ActionResult Index()
{
var GradesList = new List<SelectListItem>();
for (int i = 5; i <= 10; i++)
{
GradesList.Add(new SelectListItem { Text = i.ToString(), Value = i.ToString() });
}
StudentCourse_VM vm = new StudentCourse_VM();
vm.CourseList = new List<Course>() {
new Course(){ CourseID= 6, CourseName = "CA", CourseCode="C001"},//I will test this line(CourseList[0]) in view
new Course(){ CourseID= 7, CourseName = "CB", CourseCode="C002"},
new Course(){ CourseID= 8, CourseName = "CC", CourseCode="C003"},
};
vm.GradeItems = GradesList;
return View(vm);
}
View:
@model TestApplication1.Controllers.HomeController.StudentCourse_VM
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
//this is wrong,it shows 5
@Html.DropDownListFor(m => m.CourseList[0].CourseID, Model.GradeItems)
//this is right,it shows 6
@Html.DropDownListFor(m => m.CourseList[0].CourseID, Model.GradeItems.Select(y => new SelectListItem { Value = y.Value, Text = y.Text, Selected = (y.Value == Model.CourseList[0].CourseID.ToString()) }).ToList())
How my demo works:
Best Regards.
Yuki Tao
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.
Contributor
3710 Points
1431 Posts
Re: How the value in dropdownlist on edit page?
Oct 05, 2018 09:25 AM|Yuki Tao|LINK
Hi tricman10,
Actually,this is a Inline problem,when the parameter is one of a multi-line collection,the dropdownlistfor does not know what should be selected.
So dropdownlistfor can only display the first one option of the GradesList(5) rather than 6.
I make a demo,you could refer to it:
Model:
public class StudentCourse_VM { public List<Course> CourseList { get; set; } public IEnumerable<SelectListItem> GradeItems { get; set; }//5,6,7,8,9,10... } public class Course { public int CourseID { get; set; } public string CourseName { get; set; } public string CourseCode { get; set; } }
Controller:
View:
How my demo works:
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.