在我的Web应用程序中,我使用一个视图(Create.cshtml
)来显示两种不同的表单,这些表单将数据发布到单独的模型中。您可以在“创建”页面上看到的表单将发布到Check
模型。另一个表单呈现为部分视图 - 用户单击一个按钮,每次单击时,表单都会添加到页面中 - 此表单会发布到CheckFolders
模型。
这个想法是,'检查'包含' CheckFolders'。用户可以使用许多CheckFolders创建一个Check。提交两个表单时,应将新创建的支票的ID添加到每个CheckFolder (CheckId)
。
我面临的问题是将两个表单的数据发布到相关模型中 - 并一次提交多个CheckFolder表单。
Create.cshtml:
@model Application_Support_Dashboard.Models.Check
@{
ViewBag.Title = "Create";
Html.HiddenFor(x => x.CheckId);
Html.HiddenFor(x => x.ScheduleId); }
<h2>Create</h2>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Check</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.NTO, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.NTO, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.NTO, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.DateScheduledOn, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.DateScheduledOn, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.DateScheduledOn, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.DateCompletedBy, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.DateCompletedBy, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.DateCompletedBy, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Notes, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Notes, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Notes, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ScheduleName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ScheduleName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ScheduleName, "", new { @class = "text-danger" })
</div>
</div>
<div id="divPartial">
<!-- Partial views appear here-->
</div>
<input type="button" id="AddFolderCheck" value="Add new Item" />
<script>
$("#AddFolderCheck").click(function (e) {
$.ajax({
url: 'AddNewFolderCheck',
type: 'POST',
data: JSON.stringify('@Model'),
contentType: 'application/json; charset=utf-8',
success: function (partialview) {
$('#divPartial').append(partialview);
}
});
});
</script>
<br />
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" id="submitButton"/>
</div>
</div>
</div> }
<input type='number' size='10' id='numberinput' name='mynumber' value='0' step="1" />
<div>
@Html.ActionLink("Back to List", "Index") </div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval") }
_PartialCheckFolderModel:
@model Application_Support_Dashboard.Models.CheckFolder
<table>
<tr>
<td>@Html.LabelFor(model => model.FolderName)</td>
<td>
<input type="text" name="FolderName" />
</td>
<td>@Html.LabelFor(model => model.NumberOfFiles)</td>
<td>
<input type="text" name="NumberOfFiles" />
</td>
<td>@Html.LabelFor(model => model.FilesResolved)</td>
<td>
<input type="text" name="FilesResolved" />
</td>
</tr>
</table>
所有数据都将插入SQL数据库(Application_Support_DashboardContext)。
很抱歉,如果我没有提供足够的信息/代码 - 如果有,请告诉我。
提前致谢!