gpt4 book ai didi

c# - MVC : POST controller method get empty Dictionary

转载 作者:行者123 更新时间:2023-11-30 20:33:43 24 4
gpt4 key购买 nike

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

型号:

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

查看:

@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?

最佳答案

字典没有,列表/数组有,但你必须做一些修改。

修改模型

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

更新 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 现在应该在您将其发布到 Controller 时填充

关于c# - MVC : POST controller method get empty Dictionary,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39937281/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com