我有一个带有订单/ odc请求表单的ASP.NET MVC程序。我有一个客户,订单和订单项模型。我想做的是允许用户下订单以批准项目列表。我将viewmodel传递给包含几个字段(包括订单项对象列表)的表单/视图。我能够向显示订单项列表的表中动态添加行,但是在提交时,viewmodel列表中没有任何内容。我究竟做错了什么?如何将输入表中的项目传递到视图,以便可以提交到数据库?
控制器
public ActionResult NewOdc()
{
var viewModel = new NewOdcViewModel()
{
OdcItems = new List<tblOdcItem>()
};
viewModel.OdcItems.Add(new tblOdcItem());
return View(viewModel);
}
我从jQuery调用此代码以将新项目添加到列表中:
public ActionResult GetView(string rowCount)
{
tblOdcItem item = new tblOdcItem();
return PartialView("_OdcItemEditor", item);
}
提交后,我将此代码称为:
[HttpPost]
public ActionResult NewOdcSubmit(NewOdcViewModel viewModel)
{
_context.tblOdcs.Add(new tblOdc());
...
我正在使用foreach浏览列表并为每个项目创建一个局部。
查看:
@using (Html.BeginForm("NewOdcSubmit", "Odc", FormMethod.Post))
{
if (Model != null)
{
@Html.HiddenFor(m => m.OdcItems);
}
<div class="panel panel-info">
<div class="panel-heading">
<h2 class="panel-title">Enter New ODC</h2>
</div>
<div class="panel-body">
@Html.AntiForgeryToken()
<div class="form-group">
@Html.LabelFor(model => model.User.UserName, new { @class = "col-md-2 col-sm-1 control-label" })
<div class="col-md-2 col-sm-3">
@Html.TextBoxFor(model => model.User.UserName, new { @Value = ((PM_Portal2020.Models.tblUser)Session["User"]).UserName, @readonly = true })
</div>
@Html.LabelFor(model => model.User.Phone, new { @class = "col-md-2 col-sm-1 control-label" })
<div class="col-md-2 col-sm-3">
@Html.TextBoxFor(model => model.User.Phone, new { @Value = ((PM_Portal2020.Models.tblUser)Session["User"]).Phone })
</div>
</div>
<div class="form-group col-md-10 col-sm-12">
<label>Expenses</label>
<table id="submissionTable" class="table table-bordered">
<thead>
<tr>
<th>Qty</th>
<th>Description</th>
<th>Estimated Cost</th>
</tr>
</thead>
<tbody>
@foreach (PM_Portal2020.Models.tblOdcItem item in Model.OdcItems)
{
@Html.Partial("_OdcItemEditor", item)
}
</tbody>
</table>
<p>
<button id="add" type="button" class="btn btn-primary">Add</button>
</p>
</div>
<div class="form-group col-lg-10 col-sm-12">
@Html.LabelFor(model => model.Details, new { @class = "col-md-2 col-sm-1 control-label" })
<div class="">
@Html.TextAreaFor(model => model.Details, new { @class = "form-control" })
</div>
</div>
</div>
<div class="panel-footer">
<div class="">
<button type="submit" class="btn btn-success">Save</button>
@Html.ActionLink("Back", "Index")
</div>
</div>
</div>
}
Shared
文件夹中的PartialView:
@model PM_Portal2020.Models.tblOdcItem
<tr @Html.Id("tablerow" + Model.ID)>
<td>
<div class="editor-field">
@Html.TextBoxFor(model => model.Quantity, new { @class = "text-box single-line", name = "Quantity[" + Model.ID + "]", type = "text", value = "", required = "required" })
</div>
</td>
<td>
<div class="editor-field">
@Html.TextBoxFor(model => model.Description, new { @class = "text-box single-line", name = "Description[" + Model.ID + "]", type = "text", value = "", required = "required", id = "itemDesc" })
</div>
</td>
<td>
<div class="editor-field">
@Html.TextBoxFor(model => model.EstimatedCost, new { @class = "text-box single-line", name = "EstimatedCost[" + Model.ID + "]", type = "text", value = "", required = "required" })
</div>
</td>
<td>
<button type="button" class="btn btn-primary" onclick="removeTr(this);">
<span class="glyphicon glyphicon-trash"></span>
</button>
</td>
</tr>
查看模型
public class NewOdcViewModel
{
public NewOdcViewModel()
{
}
public IList<tblOdcItem> OdcItems { get; set; }
public string Details { get; set; }
public int OdcId { get; set; }
public tblUser User { get; set; }
}
它提交给控制器,但是odcitems列表始终为count =0。任何帮助都将非常有用。谢谢
答案 0 :(得分:1)
这是一个JavaScript示例,只需在添加/删除操作中使用此功能即可重新排列名称。
function RearangeName(){
var i = 0;
$("#submissionTable>tbody>tr").each(function () {
$(this).find("input").each(function () {
if ($(this).prop("name").indexOf('Quantity') > 0) {
$(this).attr('name', "OdcItems[" + i + "].Quantity");
}
if ($(this).prop("name").indexOf('Description') > 0) {
$(this).attr('name', "OdcItems[" + i + "].Description");
}
if ($(this).prop("name").indexOf('EstimatedCost') > 0) {
$(this).attr('name', "OdcItems[" + i + "].EstimatedCost");
}
});
i++;
});
}
答案 1 :(得分:0)
该名称应与模型属性匹配,因此在部分视图中,您将名称设置为 OdcItems [0] .Quantity ,而不是Quantity [“ + Model.ID +”]。>
@Html.TextBoxFor(model => model.Quantity, new { @class = "text-box single-line", name = "OdcItems[0].Quantity", type = "text", value = "", required = "required" })
例如 OdcItems [0]。数量
OdcItems [1]。数量
OdcItems [2]。数量
....
OdcItems [n]。数量