本文介绍了MVC:POST控制器方法获取空字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的控制器的GET方法构造并填充Model(包括Dictionary<int, MyClass>
),并将其传输到View.但是之后,POST控制器方法就不会得到带有空Dictionary的null模型.
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.
型号:
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?
How I can transmit data inside dictionary from View to Controller?
推荐答案
字典否,列表/数组是,但是您必须进行一些修改.
Dictionary no, List/Array yes, but you will have to make some modifications.
修改模型
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
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);
}
更新视图
<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
现在应在将其发布到控制器时填充
CheckBoxDataItems
should be populated now when you post it to controller
这篇关于MVC:POST控制器方法获取空字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!