Asp.net mvc 如何为列表属性创建多个内嵌表单
我想创建一个视图,用户可以在其中同时更新(创建/编辑)多个日记账条目(每个日记账一个) 视图模型:Asp.net mvc 如何为列表属性创建多个内嵌表单,asp.net-mvc,Asp.net Mvc,我想创建一个视图,用户可以在其中同时更新(创建/编辑)多个日记账条目(每个日记账一个) 视图模型: public class CompanyRecoMonthViewModel { public Company Company { get; set; } public string RecoMonth { get; set; } } 公司有一份日记账清单: public virtual IList<Journal> Journals { get; set; } 公
public class CompanyRecoMonthViewModel
{
public Company Company { get; set; }
public string RecoMonth { get; set; }
}
公司有一份日记账清单:
public virtual IList<Journal> Journals { get; set; }
公共虚拟IList日志{get;set;}
每个日志都有一个日志列表
public IList<JournalEntry> JournalEntries { get; set; }
公共IList日志{get;set;}
在每个月,日志将有一个(或没有)日志尝试
我正在从控制器将日志加载到视图中
现在查看视图中的代码。我试图将内联表单放入表视图中。
我的问题是,在提交表单时,没有一个值被捕获
可能没有正确使用@Html.BeginForm。似乎表单不知道要编辑哪个对象
<table class="table">
<tr>
<th>
Person Responsible
</th>
<th>
Journal Number
</th>
<th>
SomeID
</th>
<th>
Status
</th>
<th>
Upload Date
</th>
<th>
Amount<
</th>
<th></th>
</tr>
@foreach (var item in Model.Company.Journals)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.User.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.JournalNumber)
</td>
<!-- this is where we start inline form for Journal Entry-->
@using (Html.BeginForm("Edit", "JournalEntries", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
<form class="form-inline">
@Html.HiddenFor(modelItem => item.JournalID)
<div class="form-group">
<td>
@Html.TextBoxFor(modelItem => item.JournalEntries[0].SomeID, new { @class = "form-control", maxlength = "8" })
</td>
<td>
@Html.DropDownListFor(modelItem => item.JournalEntries[0].Status, Model.JournalStatuses.Select(e => new SelectListItem { Text = e }), string.Empty, new { @class = "form-control" })
</td>
<td>
@Html.EditorFor(modelItem => item.JournalEntries[0].DatePosted, new { htmlAttributes = new { @class = "form-control datepicker" } })
</td>
<td>
@Html.DropDownListFor(modelItem => item.JournalEntries[0].JournalType, Model.JournalTypes.Select(e => new SelectListItem { Text = e }), string.Empty, new { @class = "form-control" })
</td>
<td>
@Html.EditorFor(modelItem => item.JournalEntries[0].AmountPosted, new { htmlAttributes = new { @class = "form-control" } })
</td>
<td>
<button type="submit" class="btn btn-default">Save</button>
</td>
</div>
</form>
}
</tr>
}
</table>
负责人
日记账编号
某物
地位
上传时间
数量<
@foreach(模型.公司.日记帐中的var项目)
{
@DisplayFor(modelItem=>item.User.Name)
@DisplayFor(modelItem=>item.JournalNumber)
@使用(Html.BeginForm(“Edit”、“JournalEntries”、FormMethod.Post、new{enctype=“multipart/formdata”}))
{
@Html.AntiForgeryToken()
@Html.HiddenFor(modeleItem=>item.JournalID)
@Html.TextBoxFor(modelItem=>item.JournalEntries[0].SomeID,新的{@class=“form control”,maxlength=“8”})
@Html.DropDownListFor(modelItem=>item.JournalEntries[0]。状态,Model.JournalStatus.Select(e=>new SelectListItem{Text=e}),字符串。空,新{@class=“form control”})
@Html.EditorFor(modeleItem=>item.JournalEntries[0]。DatePosted,new{htmlAttributes=new{@class=“form control datepicker”})
@Html.DropDownListFor(modelItem=>item.JournalEntries[0]。JournalType,Model.JournalTypes.Select(e=>new SelectListItem{Text=e}),string.Empty,new{@class=“form control”})
@EditorFor(modelItem=>item.JournalEntries[0].AmountPosted,new{htmlAttributes=new{@class=“form control”})
拯救
}
}
将virtual关键字添加到Journal类中的JournalEntries属性,以导航到JournalEntry类
public virtual IList<JournalEntry> JournalEntries { get; set; }
公共虚拟IList日志{get;set;}
通过使用多个局部视图以及每个局部视图一个实体的内联表单,成功实现了这一点。请发布您的控制器发布方法