Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>For example: The View has two form:form1 and form2。</p> <p>The form1 has two part.</p> <p>The first part: There are two textbox and a Create button and a Save button.</p> <p>The second part: There are a .</p> <p>so, when we fill in two textbox and press the Create button, if not pass validation, then show error messages. if pass validation, then insert values into the and insert values to the form2.</p> <p>[HttpPost] Action Create has a parameter List models.</p> <p>when we press the Save button, will take values of form2 to controller mapping to List models.</p> <p>Model</p> <pre><code>public class HomeModel { [Required] public string Name { get; set; } [Required] public string Number { get; set; } } </code></pre> <p>Controller:</p> <pre><code> [HttpPost] public ActionResult Create(List&lt;HomeModel&gt; models) { // TODO: Add insert logic here } </code></pre> <p>View:</p> <pre><code>@model MvcApplication1.Models.HomeModel @{ ViewBag.Title = "Create"; } &lt;h2&gt;Create&lt;/h2&gt; @using (Html.BeginForm("Create", "Home", FormMethod.Post, new { id = "form1" })) { @Html.ValidationSummary(true) &lt;fieldset&gt; &lt;legend&gt;HomeModel&lt;/legend&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.Name) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.EditorFor(model =&gt; model.Name) @Html.ValidationMessageFor(model =&gt; model.Name) &lt;/div&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.Number) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.EditorFor(model =&gt; model.Number) @Html.ValidationMessageFor(model =&gt; model.Number) &lt;/div&gt; &lt;p&gt; &lt;input type="submit" value="Create" id="btnCreate" /&gt; &lt;input type="button" value="Save" id="btnSave" /&gt; &lt;/p&gt; &lt;/fieldset&gt; &lt;table id="tb"&gt;&lt;/table&gt; } &lt;div id="FormInfo" style="display: none;"&gt; &lt;form action="/Home/Create" id="form2" method="post"&gt;&lt;/form&gt; &lt;/div&gt; &lt;div&gt; @Html.ActionLink("Back to List", "Index") &lt;/div&gt; @section Scripts { @Scripts.Render("~/bundles/jqueryval") &lt;script type="text/javascript"&gt; $(function () { $("#form1").submit(function () { var ErrorLength = $(".field-validation-error").length; if (ErrorLength &lt;= 0) { var nameValue = $("#Name").val(); var numberValue = $("#Number").val(); InsertToForm2(nameValue, numberValue); InsertToTable(nameValue, numberValue); } return false; }); $("#btnSave").click(function () { $("#form2").submit(); }); }); function InsertToForm2(nameValue, numberValue) { var inputCount = $("#form2 input").length / 2; var combineHTML = "&lt;input type='text' name='models[" + inputCount + "].Name' value='" + nameValue + "' /&gt; "; combineHTML += "&lt;input type='text' name='models[" + inputCount + "].Number' value='" + numberValue + "'/&gt; "; $("#form2").append(combineHTML); } function InsertToTable(nameValue, numberValue) { var combineHTML = "&lt;tr&gt;&lt;td&gt;" + nameValue + "&lt;/td&gt;&lt;td&gt;" + numberValue + "&lt;/td&gt;&lt;/tr&gt;"; $("#tb").append(combineHTML); } &lt;/script&gt; } </code></pre> <p><a href="http://www.mediafire.com/?8mekwbf836c8kk4" rel="nofollow">Example Download</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload