问题描述:

In my web application I am using one view (Create.cshtml) to display two different forms which post data to separate models. The form which you can see implemented on the Create page is posting to the Check model. The other form is rendered as a partial view - the user clicks a button, and on each click, the form is added to the page - this one posts to the CheckFolders model.

The idea, is that a 'Check' contains 'CheckFolders'. A user can create one Check, with many CheckFolders. When both forms are submitted, the ID of the newly created check, should be added to each CheckFolder (CheckId).

The problem I am facing is posting the data of both forms to their relevant models - and submitting more than one CheckFolder form at once.

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>

All data is to be inserted into the SQL Database (Application_Support_DashboardContext).

Sorry if I haven't provided enough information/code - please let me know if so.

Thanks in advance!

相关阅读:
Top