为清晰和保密起见,我有以下观点模型:
public class BoqReviewViewModel
{
public List<BoqReviewItemViewModel> Items { get; set; } = new List<BoqReviewItemViewModel>();
}
public static BoqReviewItemViewModel FromEntity(GTT_BOQ_IMPORT import)
{
var ret = new BoqReviewItemViewModel();
ret.BOQ_IMPORT_ID = import.BOQ_IMPORT_ID;
ret.ITEM_CODE = import.ITEM_CODE;
ret.ITEM_DESC = import.ITEM_DESC;
ret.UNIT = import.UNIT;
ret.QTY = import.QTY;
ret.RATE = import.RATE;
ret.AMOUNT = import.AMOUNT;
ret.ITEM_PAYS = import.ITEM_PAYS;
ret.ITEM_CPA = import.ITEM_CPA;
return ret;
}
public GTT_BOQ_IMPORT ToEntity()
{
var ret = new GTT_BOQ_IMPORT();
ret.BOQ_IMPORT_ID = this.BOQ_IMPORT_ID;
ret.ITEM_CODE = this.ITEM_CODE;
ret.ITEM_DESC = this.ITEM_DESC;
ret.UNIT = this.UNIT;
ret.QTY = this.QTY;
ret.RATE = this.RATE;
ret.AMOUNT = this.AMOUNT;
ret.ITEM_PAYS = this.ITEM_PAYS;
ret.ITEM_CPA = this.ITEM_CPA;
return ret;
}
[Key]
public Guid BOQ_IMPORT_ID;
public string ITEM_CODE { get; set; }
public string ITEM_DESC { get; set; }
public string UNIT { get; set; }
public decimal? QTY { get; set; }
public decimal? RATE { get; set; }
public decimal? AMOUNT { get; set; }
public bool? ITEM_PAYS { get; set; }
public bool? ITEM_CPA { get; set; }
public bool IsFlagged { get; set; }
}
然后对索引类型视图执行以下GET操作:
[HttpGet]
public ActionResult BoqReview()
{
var model = new BoqReviewViewModel();
model.CONTRACT_ID = Guid.NewGuid();
model.PROJECT_ID = Guid.NewGuid();
model.Items = GetItems().ToList();
return View(model);
}
其中GetItems
是一种返回IEnumerable的方法,该IEnumerable具有三个完全填充的项级别视图模型。启动应用程序时,我看到一个正确填充的表,其中包含3行,所有列均正确填充。
这是视图:
@using (Html.BeginForm("BoqReview", "Boq", FormMethod.Post))
{
<table class="table">
<tr>
<th>
</th>
<th class="col-header">
Item Code
</th>
<th class="col-header">
Item Description
</th>
<th>
Unit
</th>
<th>
Quantity
</th>
<th>
Rate
</th>
<th>
Amount
</th>
<th>
Item Pays
</th>
<th>
Item CPA
</th>
<th>
</th>
</tr>
@for (var i = 0; i < Model.Items.Count; i++)
{
<tr>
<td>
@Html.TextBoxFor(model => model.Items[i].BOQ_IMPORT_ID)
</td>
<td>
@Html.TextBoxFor(model => Model.Items[i].ITEM_CODE)
@Html.ValidationMessageFor(model => Model.Items[i].ITEM_CODE)
</td>
<td>
@Html.TextBoxFor(model => Model.Items[i].ITEM_DESC)
@Html.ValidationMessageFor(model => Model.Items[i].ITEM_DESC)
</td>
<td>
@Html.TextBoxFor(model => Model.Items[i].UNIT, new {style = "width: 120px;"})
@Html.ValidationMessageFor(model => Model.Items[i].UNIT)
</td>
<td>
@Html.TextBoxFor(model => Model.Items[i].QTY)
@Html.ValidationMessageFor(model => Model.Items[i].QTY)
</td>
<td>
@Html.TextBoxFor(model => Model.Items[i].RATE)
@Html.ValidationMessageFor(model => Model.Items[i].RATE)
</td>
<td>
@Html.TextBoxFor(model => Model.Items[i].AMOUNT)
@Html.ValidationMessageFor(model => Model.Items[i].AMOUNT)
</td>
<td>
@Html.DropDownListFor(model => Model.Items[i].ITEM_PAYS, Model.YesNoSelectList)
@Html.ValidationMessageFor(model => Model.Items[i].ITEM_PAYS)
</td>
<td>
@Html.DropDownListFor(model => Model.Items[i].ITEM_CPA, Model.YesNoSelectList)
@Html.ValidationMessageFor(model => Model.Items[i].ITEM_CPA)
</td>
<td>
@Html.CheckBoxFor(model => Model.Items[i].IsFlagged)
</td>
</tr>
}
<tr>
<td colspan="4">
<input type="submit" value="Submit" />
</td>
</tr>
</table>
}
一切看起来都很完美,但是当我提交表格时:
[HttpPost]
public ActionResult BoqReview(BoqReviewViewModel model)
{
using (var db = new AppDbContext())
{
foreach (var modelItem in model.Items)
{
var updated = modelItem.ToEntity();
db.GttBoqImports.AddOrUpdate(updated);
}
db.SaveChanges();
}
return RedirectToAction("BoqReview");
}
每个项目视图模型上的BOQ_IMPORT_ID
字段是空的GUID,但显示的表显示了正确的GUID。项目级视图模型的所有其他属性仍会填充。
仅此BOQ_IMPORT_ID
并没有正确绑定模型,这可能是什么问题?当我在Chrome工具中调试网络调用时,它会在发布的表单数据中为每个BOQ_IMPORT_ID
显示一个正确的GUID值:
Items[0].BOQ_IMPORT_ID: 610d9a12-920f-43aa-89bc-08b68067a462
Items[0].ITEM_CODE: AA1
Items[0].ITEM_DESC: Some A's and a 1
Items[0].UNIT: kg
Items[0].QTY: 200
...
只需输入:重命名该类中的VM属性,并在视图中解决了整个问题。也许,尽管有几次VS重新启动,但机器重新启动仍然在某种程度上有所帮助。这闻起来是临时文件或某些地方的缓存。