I know it was silly to ask this question but I\'m not able to figure it out and I need your help guys.
First of all I am new to MVC. In my Project I am using a dropdownl
I think i spotted the problem, you need to do the following two things to resolve,
1) change the model property public IList<string> InterviewerName
to public string InterviewerName
2) use ViewBag to take the selectlist values to the View.
Let me know if it helps.
I think you should update your viewmodel like this
public class InterviewViewModel
{
public List<SelectListItem> Interviewers { set;get;}
public int SelectedInterviewerID { set;get;}
//Other properties relevant to the view as needed
}
And in your GET action set the Interviewers
collection property
public ActionResult Interview()
{
var vm=new InterviewViewModel();
vm.Interviewers =GetInterViewrsFromSomewhere();
return View(vm);
}
public List<SelectListItem> GetInterViewrsFromSomewhere()
{
var list=new List<SelectListItem>();
//Items hard coded for demo. you can read from your db and fill here
list.Add(new SelectListItem { Value="1", Text="AA"});
list.Add(new SelectListItem { Value="2", Text="BB"});
return list;
}
And in your view which is strongly typed to InterviewViewModel
@model InterviewViewModel
@using(Html.Beginform())
{
<p>Select interviewer :
@Html.DropdownlistFor(x=>x.SelectedInterviewerID,Model.Interviewers,"Select")
<input type="submit" />
}
So when the form is posted, The selected interviewers id will be available in the SelectedInterviewerID
property
[HttpPost]
public ActionResult Interview(InterviewViewModel model)
{
if(ModelState.IsValid)
{
//check for model.SelectedIntervieweID value
//to do :Save and redirect
}
//Reload the dropdown data again before returning to view
vm.Interviewers=GetInterViewrsFromSomewhere();
return View(vm);
}
In the HttpPost action method, if you are returning the viewmodel back to the view, you need to refill the dropdown content because HTTP is stateless and it wont keep the dropdown content between the requests (Webforms does this useing Viewstate and here in MVC we dont have that)