ViewModel在action方法中获取null值

Dha*_*are 5 c# asp.net-mvc asp.net-mvc-4

我正在使用ViewModel来检索控制器操作中输入的数据.但ViewModel在其属性中获取空值.我正在创建一个局部视图

在该局部视图中,我通过绑定ViewModel 创建下拉列表,然后我在其他视图中渲染该局部视图

以下是我的代码

我的ViewModel:

public class LookUpViewModel
    {
        RosterManagementEntities rosterManagementContext = new RosterManagementEntities();
        public  LookUpViewModel()
        {

            tblCurrentLocations = from o in rosterManagementContext.tblCurrentLocations select o;
            tblStreams = from o in rosterManagementContext.tblStreams select o;   
        }

        [Required]
        public virtual IEnumerable<tblCurrentLocation> tblCurrentLocations { get; set; }

 [Required]
        public virtual IEnumerable<tblStream> tblStreams {  get;  set; }
Run Code Online (Sandbox Code Playgroud)

我的部分观点:

@model PITCRoster.ViewModel.LookUpViewModel

@Html.Label("Location")
@Html.DropDownListFor(M=>M.tblCurrentLocations, new SelectList(Model.tblCurrentLocations, "LocationId", "Location"), "Select Location")
@Html.ValidationMessageFor(M=>M.tblCurrentLocations)
<br />
@Html.Label("Stream")

@Html.DropDownListFor(M => M.tblStreams, new SelectList(Model.tblStreams, "StreamId", "Stream"), "Select Streams")
@Html.ValidationMessageFor(M => M.tblStreams)
Run Code Online (Sandbox Code Playgroud)

我的视图,我在上面呈现部分视图

@{
    ViewBag.Title = "Resources";
}
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
<h2>Resources</h2>

@using (Html.BeginForm("AddResource", "Resources", FormMethod.Post))
{

    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)
    Html.RenderPartial("_LookUpDropDowns", new PITCRoster.ViewModel.LookUpViewModel());

    <br />
    <input type="submit" value="Create" />
}
Run Code Online (Sandbox Code Playgroud)

这是我的控制器动作方法:[HttpPost]

public void AddResource(LookUpViewModel testName)
        {
            //code
        }
Run Code Online (Sandbox Code Playgroud)

当我在我的控制器上放置调试器时,操作方法控制转到该Action方法.但ViewModel对象中包含null.我尝试使用FormCollection对象访问输入的值,我按预期获得所有数据...

下面是我使用FormCollection进行控制器操作的代码

 [HttpPost]
        public void AddResource(FormCollection form)
        {
            var x = form["tblStreams"]; //I get value here..
        }
Run Code Online (Sandbox Code Playgroud)

任何人都可以解释为什么我没有在ViewModel对象中获取值?谢谢...

小智 4

您无法将下拉列表绑定到复杂对象的集合 - 在您的情况下IEnumerable<tblCurrentLocation>IEnumerable<tblStream>

标签<select>仅回发单个值(所选选项的值),因此在 POST 方法中DefaultModelBinder尝试这样做testName.tblCurrentLocations = "1"(假设所选选项的值是1),这当然会失败并且属性设置为null

您需要一个包含要绑定的属性的视图模型(理想情况下将包括SelectList助手使用的DropDownListFor()

public class LookUpViewModel
{
  [Display(Name = "Location")]
  [Required(ErrorMessage = "Please select a location")]
  public int SelectedLocation { get; set; }
  [Display(Name = "Stream")]
  [Required(ErrorMessage = "Please select a stream")]
  public int SelectedStream { get; set; }
  public SelectList LocationList { get; set; }
  public SelectList StreamList { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

然后在视图中

@Html.LabelFor(m => m.SelectedLocation)
@Html.DropDownListFor(m => m.SelectedLocation, Model.LocationList, "-Please select-")
@Html.ValidationMessageFor(m => m.SelectedLocation)

@Html.LabelFor(m => m.SelectedStream)
@Html.DropDownListFor(m => m.SelectedStream, Model.StreamList, "-Please select-")
@Html.ValidationMessageFor(m => m.SelectedStream)
Run Code Online (Sandbox Code Playgroud)

并在控制器中

public ActionResult Edit()
{
  LookUpViewModel model = new LookUpViewModel();
  ConfigureViewModel(model);
  return View(model);
}

[HttpPost]
public ActionResult Edit(LookUpViewModel model)
{
  if (!ModelState.IsValid)
  {
    ConfigureViewModel(model);
    return View(model);
  }
  // model.SelectedLocation will contain the value of the selected location
  // save and redirect
}

private void ConfigureViewModel(LookUpViewModel model)
{
  // populate your select lists
  var locations = from o in rosterManagementContext.tblCurrentLocations select o;
  model.LocationList = new SelectList(locations, "LocationId", "Location");
  .... // ditto for streams
}
Run Code Online (Sandbox Code Playgroud)

请注意,Maximilian 的回答也指出,您的视图模型应该只包含视图所需的属性。您的控制器负责填充值。视图模型永远不应该调用数据库——它甚至不应该知道数据库的存在。