我正在尝试使用ASP.Net MVC(C#)发布表单。发布表单时,没有数据到达控制器。视图模型保持其初始化状态。
您可以在下面看到发布的表单数据(从Chrome开发人员工具中复制)但是当它到达控制器时,那里什么都没有:
Triage.ClaimNumber:Test-001
Triage.PropertyAgeId:1
Triage.RoomNumber:1
Triage.Rooms[0].RoomHeight:3
Triage.Rooms[0].RoomWidth:3
Triage.Rooms[0].RoomLength:4
Triage.Rooms[0].CleaningRequired:False
Triage.Rooms[0].DryingRequired:False
Triage.Rooms[0].CeilingDamaged:True
Triage.Rooms[0].WallsDamaged:False
Triage.Rooms[0].FloorDamaged:False
Triage.Rooms[0].KitchenDamaged:False
Triage.Rooms[0].BathroomDamaged:False
Triage.Rooms[0].Ceiling.CeilingTypeId:66
Triage.Rooms[0].Ceiling.AmountDamaged:0.10
Triage.Rooms[0].Ceiling.Papered:False
名称约定适用于模型绑定自动执行,任何人都可以帮助我解释数据未被发布的原因吗?
以下是viewmodels的示例:
天花板视图模型
public class TriageCeilingViewModel
{
[DisplayName("Ceiling type")]
public int CeilingTypeId { get; set; }
[Required(ErrorMessage = "Please enter the amount of damage to the ceiling")]
[DisplayName("% Amount damaged")]
public decimal AmountDamaged { get; set; }
......
}
分类视图模型
public class TriageRoomViewModel
{
private Repository _data = new Repository();
public List<CeilingTypeList> CeilingTypes
{
get
{
return _data.GetCeilingTypes();
}
}
[Required()]
[DisplayName("Height (m)")]
public decimal RoomHeight { get; set; }
[Required(ErrorMessage = "Is the ceiling damaged?")]
[DisplayName("Ceiling damaged?")]
public bool CeilingDamaged { get; set; }
public TriageCeilingViewModel Ceiling = new TriageCeilingViewModel();
........
}
索引视图模型
public class TriageViewModel
{
private readonly Repository _data = new Repository();
public List<PropertyAgeList> PropertyAges
{
get
{
return _data.GetPropertyAges();
}
}
[Required(ErrorMessage = "Please enter the claim number")]
[StringLength(12, ErrorMessage = "The claim number must be 12 digits or less")]
[DisplayName("Claim Number")]
public string ClaimNumber { get; set; }
public List<TriageRoomViewModel> Rooms = Enumerable.Range(0, 4).Select(i => new TriageRoomViewModel()).ToList();
........
}
TriageIndexViewModel
public class TriageIndexViewModel
{
public TriageViewModel = new TriageViewModel();
}
Index.cshtml
@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
@Html.AntiForgeryToken()
@for (int i = 0; i < 4; i++)
{
ViewBag.RoomNumber = i;
@Html.EditorFor(m=>m.Triage.Rooms[i])
}
}
TriageRoomViewModel.cshtml
@model BuildingsTriage.TriageRoomViewModel
<div class="panel-body">
<div class="row">
<div class="col-md-12">
<div class="row">
<div class="col-md-4 form-group @(ViewData.ModelState.IsValidField(Html.IdFor(m=>m.RoomHeight).ToString()) ? null : "has-error")">
@Html.LabelFor(m => m.RoomHeight, new { @class="room" + i + " control-label" })
@Html.EditorFor(m => m.RoomHeight, new { htmlAttributes = new { @class="room" + i + " form-control", type = "text" }, })
@Html.ValidationMessageFor(m => m.RoomHeight)
</div>
<div class="col-md-4 form-group @(ViewData.ModelState.IsValidField(Html.IdFor(m=>m.RoomWidth).ToString()) ? null : "has-error")">
@Html.LabelFor(m => m.RoomWidth, new { @class="room" + i + " control-label" })
@Html.EditorFor(m => m.RoomWidth, new { htmlAttributes = new { @class="room" + i + " form-control", type = "text" }, })
@Html.ValidationMessageFor(m => m.RoomWidth)
</div>
<div class="col-md-4 form-group @(ViewData.ModelState.IsValidField(Html.IdFor(m=>m.RoomLength).ToString()) ? null : "has-error")">
@Html.LabelFor(m => m.RoomLength, new { @class="room" + i + " control-label" })
@Html.EditorFor(m => m.RoomLength, new { htmlAttributes = new { @class="room" + i + " form-control", type = "text" }, })
@Html.ValidationMessageFor(m => m.RoomLength)
</div>
</div>
</div>
</div>
答案 0 :(得分:1)
您正在使用字段而不是属性。由于TriageIndexViewModel
不是属性,因此没有任何降低可以正确绑定。
您需要将所有字段更改为属性,并将其初始化移动到零参数构造函数中。
答案 1 :(得分:0)
您的index post方法接受TriageIndexViewModel。您尚未在上面的视图模型中对此进行定义。 这可能是问题吗?你的控制器是期待这个视图模型,但你在表单上传递了另一个?