using DropdownlistFor helper for a list of names

ε祈祈猫儿з 提交于 2019-12-02 09:25:26

问题


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 dropdownlistFor helper for displaying a list of names available for a particular Id. I did that and it is displaying names for an id.

Now while posting the form I am getting a Null Reference Exception for property used in the dropdownlist.

Here is my property in model which is a list of names.

In my controller in the [HttpGet] I did this which calls a function and returns a list of names for that Id.

Now the list of names is being displyed while form loading. And my view is as

When I am submitting the form I am getting a Null Reference Exception because in new SelectList(Model.InterviewerName) the Model is NULL.

Is there anyway to get me out of this issue.


回答1:


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)




回答2:


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.



来源:https://stackoverflow.com/questions/18852331/using-dropdownlistfor-helper-for-a-list-of-names

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!