使用DropdownlistFor帮手的名单 [英] using DropdownlistFor helper for a list of names

查看:111
本文介绍了使用DropdownlistFor帮手的名单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这是愚蠢的,问这个问题,但我无法弄清楚,我需要你的帮助球员。

I know it was silly to ask this question but I'm not able to figure it out and I need your help guys.

所有的我是新来的MVC第一。在我的项目我使用的是dropdownlistFor帮手用于显示一个特定的ID可用名称的列表。我这样做,它是一个ID显示的名字。

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.

现在,而张贴的形式我得到了在DropDownList使用物业的空引用异常。

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.

在我的控制器中的[HTTPGET]我做这里面调用一个函数,并返回名称列表该ID。

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

当我提出我得到一个空引用异常,因为在新的SelectList(Model.InterviewerName)的型号是空的形式。

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.

推荐答案

我觉得你应该更新您的视图模型像这样

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
}

和中设置的面试官集合属性的GET操作

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;
}

和在你看来这是强类型为 InterviewViewModel

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" />
}

因此​​,当形式发布,选定面试ID将在 SelectedInterviewerID 特提供

[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);
}

在HttpPost操作方法,如果你正在返回的视图模型回看,你需要补充的下拉含量因为HTTP是无状态的,它不会保持请求之间的下拉列表内容(Web表单这是否期运用视图状态,并在这里MVC我们没有了)

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)

这篇关于使用DropdownlistFor帮手的名单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆