MVC:POST控制器方法获取空字典

时间:2016-10-08 20:56:01

标签: c# asp.net-mvc dictionary asp.net-mvc-5

我的GET控制器构造方法和填充模型,包括Dictionary<int, MyClass>,并将其传输给View。但之后,POST控制器方法得到的空模型不是空模型。

型号:

  public class CheckBoxItem
  {
    public string Name { get; set; }
    public double Data { get; set; }
    public bool Selected { get; set; }
  }
  public class CreateNewEventModel
  {
    [Required(ErrorMessage = "Error text")]
    [Display(Name = "Header name")]
    public string EventName { get; set; }
    public Dictionary<int, CheckBoxItem> CheckBoxDataItems { get; set; }
    public CreateNewEventModel()
    {
      CheckBoxDataItems = new Dictionary<int, CheckBoxItem>();
    }
  }

控制器:

public ActionResult CreateEvent()
    {
      CreateNewEventModel model = new CreateNewEventModel();
      // FILL MODEL
      foreach (var user in db.UsersInfo.ToList())
      {
        model.CheckBoxDataItems.Add(user.Id, new CheckBoxItem()
        {
          Name = user.Name,
          Data = 0,
          Selected = false
        });
      }
      // THERE IS FULL MODEL
      return View(model);
    }
    [HttpPost]
    public ActionResult CreateEvent(CreateNewEventModel model)
    {
      // THERE IS model.Event name include text
      // BUT model.CheckBoxDataItems is empty
      if (ModelState.IsValid)
      {
        ...    
        return View(model);
      }
      return View(model);
    }

查看:

@model HLyaa.Models.CreateNewEventModel

@{
    ViewBag.Title = "Create Event";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Создание события</h2>

@if (Model.CheckBoxDataItems.Count() != 0)
{
  using (Html.BeginForm("CreateEvent", "Events", FormMethod.Post))
  {
    @Html.ValidationSummary()
    <div>
      @Html.LabelFor(model => model.EventName)
      <div>
        @Html.EditorFor(model => model.EventName)
      </div>
    </div>
    <table>
      @foreach (var kvpair in Model.CheckBoxDataItems)
      {
        <tr>
          <td>
            @Html.CheckBoxFor(model => model.CheckBoxDataItems[kvpair.Key].Selected)
          </td>
          <td>
            @Html.DisplayFor(model => model.CheckBoxDataItems[kvpair.Key].Name)
            @Html.HiddenFor(model => model.CheckBoxDataItems[kvpair.Key].Selected)            
            @Html.HiddenFor(model => model.CheckBoxDataItems[kvpair.Key].Name)         
          </td>
          <td>
            @Html.TextBoxFor(model => model.CheckBoxDataItems[kvpair.Key].Data, new { @type = "number" })
          </td>
        </tr>
      }
    </table>
    <br />
    <input type="submit" value="Next" />
  }
}

如何在字典中将数据从View传输到Controller?

1 个答案:

答案 0 :(得分:3)

字典否,列表/数组是,但您必须进行一些修改。

修改模型

public class CheckBoxItem {
    public int UserId { get; set; }
    public string Name { get; set; }
    public double Data { get; set; }
    public bool Selected { get; set; }
}

public class CreateNewEventModel {
    [Required(ErrorMessage = "Error text")]
    [Display(Name = "Header name")]
    public string EventName { get; set; }
    public List<CheckBoxItem> CheckBoxDataItems { get; set; }
    public CreateNewEventModel() {
        CheckBoxDataItems = new List<CheckBoxItem>();
    }
}

修改GET方法CreateEvent

public ActionResult CreateEvent() {
    var model = new CreateNewEventModel();
    //...FILL MODEL
    foreach (var user in db.UsersInfo.ToList()) {
        model.CheckBoxDataItems.Add(new CheckBoxItem() {
            UserId = user.Id,
            Name = user.Name,
            Data = 0,
            Selected = false
        });
    }
    // THERE IS FULL MODEL
    return View(model);
}

更新视图

<table>
  @for (var i = 0; i < Model.CheckBoxDataItems.Count; i++) {
    <tr>
      <td>
        @Html.CheckBoxFor(model => model.CheckBoxDataItems[i].Selected)
      </td>
      <td>
        @Html.DisplayFor(model => model.CheckBoxDataItems[i].Name)
        @Html.HiddenFor(model => model.CheckBoxDataItems[i].UserId)
        @Html.HiddenFor(model => model.CheckBoxDataItems[i].Selected)
        @Html.HiddenFor(model => model.CheckBoxDataItems[i].Name)
      </td>
      <td>
        @Html.TextBoxFor(model => model.CheckBoxDataItems[i].Data, new { @type = "number" })
      </td>
    </tr>
  }
</table>
当你将它发布到控制器

时,现在应该填充

CheckBoxDataItems