MVC: POST controller method get empty Dictionary

2019-07-25 10:01发布

My GET method of controller construct and fill Model, which including Dictionary<int, MyClass>, and transmit that to View. But after, POST controller method get not null model with empty Dictionary.

Model:

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

Controller:

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

View:

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

How I can transmit data inside dictionary from View to Controller?

1条回答
smile是对你的礼貌
2楼-- · 2019-07-25 10:51

Dictionary no, List/Array yes, but you will have to make some modifications.

Modify models

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

Modify GET method 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);
}

Update View

<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 should be populated now when you post it to controller

查看更多
登录 后发表回答