i am getting a problem while populating data inside the dropdown…
I guess since i am using a partial view that is why it is creating problem.
Here’s my code:
public ActionResult Register()
{
var course = from Course c in Enum.GetValues(typeof(Course))
select new { ID = c, Name = c.ToString() };
ViewData["course"] = new SelectList(course, "ID", "Name");
return view();
}
public enum Course
{
[Display(Name = "Basic Level", ShortName = "Basic")]
Basic = 1,
[Display(Name = "Intermidiate Level")]
Intermidiate = 2,
[Display(Name = "Advance Level")]
Advance = 3
}
This is my Main view:
<table style="margin-top: 10px">
<tr>
<td valign="top">
@{Html.RenderPartial("LogOnPartial");}
</td>
<td width="20px">
</td>
<td align="left">
@{Html.RenderPartial("CreatePartial");}
</td>
</tr>
I have written this in my partial view(CreatePartial):
<td>
@Html.DropDownList("course");
</td>
Please help me
This should work. I am unable to reproduce the problem.
Model:
Controller:
Index.cshtmlview:CreatePartial.cshtmlpartial:I suspect that this is not working after you submit the form, not when it is initially loaded. And this might happen because you probably forgot to repopulate the
ViewData["course"]in your[HttpPost]action and yet tried to redisplay the same view. If you want to redisplay the same view make sure that you fill theViewData["course"]the same way you did in your GET action that was used to initially render the page.Another thing to watch out is that you don’t have some other
ViewData["course"]value that is conflicting.