问题描述
我是.net的新手。我有这种模型,对我来说已经好几天了。
类 DetailedRecordModel
public class DetailedRecordModel
{
public string RecordID { get; set; }
public string EmployeeID { get; set; }
public string CustomerID { get; set; }
[DataType(DataType.Date)]
public string InitDate { get; set; }
[DataType(DataType.Date)]
public string DeliveryDate { get; set; }
public virtual ICollection<PurchaseDetail> detail{ get; set; }
}
类别购买详细信息
public class PurchaseDetail
{
public string ProductID { get; set; }
public int Qty { get; set; }
public double price { get; set; }
public string RecordID { get; set; }
}
控制器
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(DetailedRecordModel record)
{
if (ModelState.IsValid)
{
return View(record);
}
return RedirectToAction("ViewRecords");
}
html
<div class="form-group">
@Html.LabelFor(model => model.EmployeeID,htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.EmployeeID,(IEnumerable<SelectListItem>)ViewData["sellistemp"])
@Html.ValidationMessageFor(model => model.EmployeeID,"",new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.CustomerID,htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.CustomerID,(IEnumerable<SelectListItem>)ViewData["sellistcust"])
@Html.ValidationMessageFor(model => model.CustomerID,new { @class = "text-danger" })
</div>
</div>
<tr>
<td style="display:none" id="Index0" name="detail.Index" value="0"></td>
<td>1</td>
<td id="ProductID" name="detail[0].ProductID" value="sp00002">sp00002</td>
<td id="Qty" name="detail[0].Qty" value="12123">12123</td>
<td id="price" name="detail[0].price" value="2312">2312</td>
</tr>
<tr>
<td style="display:none" id="Index1" name="detail.Index" value="1"></td>
<td>2</td>
<td id="ProductID" name="detail[1].ProductID" value="sp00003">sp00003</td>
<td id="Qty" name="detail[1].Qty" value="2323">2323</td>
<td id="price" name="detail[1].price" value="3223">3223</td>
</tr>
将传递给控制器的RecordID,EmployeeID,CustomerID,InitDate和DeliveryDate都可以,但是对于<PurchaseDetail> detail
,我总是得到null。我该如何解决这个问题?
解决方法
您必须使用View(myModel)
或RedirectToAction("ViewRecords",record)
将模型传递给视图
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(DetailedRecordModel record)
{
if (ModelState.IsValid)
{
return RedirectToAction("ViewRecords",record);
}
return View(record);
}
public IActionResult ViewRecords(DetailedRecordModel model)
{
return View(model);
}
然后在视图中,您可以像此处How to pass model in MVC view
那样访问模型- 在
@model DetailedRecordModel;
顶部添加模型的定义 - 添加模型后,您可以使用
@Model
(以html格式)或Model
访问文件中的所有位置
@model DetailedRecordModel;
@{
ViewData["Title"] = "ViewRecords";
}
<h1>ViewRecords</h1>
<div class="form-group">
@Html.LabelFor(model => model.EmployeeID,htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.EmployeeID,(IEnumerable<SelectListItem>)ViewData["sellistemp"])
@Html.ValidationMessageFor(model => model.EmployeeID,"",new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.CustomerID,htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.CustomerID,(IEnumerable<SelectListItem>)ViewData["sellistcust"])
@Html.ValidationMessageFor(model => model.CustomerID,new { @class = "text-danger" })
</div>
</div>
@foreach (var entry in Model.detail)
{
<tr>
<td style="display:none" id="Index0" name="detail.Index" value="0"></td>
<td>1</td>
<td id="ProductID" name="@entry.ProductID" value="sp00002">sp00002</td>
<td id="Qty" name="@entry.Qty" value="12123">12123</td>
<td id="price" name="@entry.price" value="2312">2312</td>
</tr>
}
,
经过2天痛苦的绝望,我知道为了绑定值,我通过在模型CREATE TABLE IF NOT EXISTS assets (
asset_id INTEGER PRIMARY KEY AUTOINCREMENT,history_id INTEGER FOREIGN KEY);
CREATE TABLE IF NOT EXISTS assets (
asset_id INTEGER PRIMARY KEY AUTOINCREMENT,history_id INTEGER FOREIGN KEY);
中声明一个对象DetailedRecordModel
找到了答案,我必须更改每个{{ 1}}标签插入PurchaseDetail