简体   繁体   中英

c# asp.net mvc One Button to submit data from multiple forms on one view

I have one view that has 2 forms on. I wish to have 1 button outside of those forms that submits the data from both forms in one go. I can submit data using a button within each form. Is there a way to press a button outside of both forms that can access the data from both forms for sending to a database? My code for the view is below. The buttons FORM1 saves the 1st forms data, FORM2 saves the 2nd forms data, but I want FORM-ALL button to save the data of both forms when it seems to not have access to any of the forms data. Please note the IsPost method was trying to put a global variable as one of the forms textboxes but it doesn't seem to work, probably because a post is not occuring for the FORM-ALL button?

@model IEnumerable<App22.Models.Cust>
@{
ViewBag.Title = "Customer Details";
}

@{
if (IsPost)
{
    GlobalVar.GlobCustName2 = Request.Form["B2CustName"];

    <text>
        You entered:
        <br />
        @GlobalVar.GlobCustName2
        <br />
    </text>
}
}


<h2>@ViewBag.Title.</h2>

<header>
</header>
<meta name="viewport" content="width=device-width" />
<title>CustViewy</title>

<html>
<head>
</head>

<style>
th, td {
    padding: 5px;
}
</style>

<body>
<p>

</p>
<div class="row">
    <div class="col-md-4">
        <p>
            <button style="background-color:blue" type="button" name="tree" class="btn btn- 
 primary" onclick="location.href='@Url.Action("Index","Cust1")'">
                BACK &raquo;
            </button>

            <button form="CustForm" style="background-color:red" type="submit" name="tree2" 
class="btn btn-primary">
                FORM1 &raquo;
            </button>

            <button form="CustForm2" style="background-color:red" type="submit" name="tree3" 
 class="btn btn-primary">
                FORM2 &raquo;
            </button>

            <button style="background-color:red" formmethod="post" type="submit" name="tree4" 
class="btn btn-primary" onclick="location.href='@Url.Action("SaveCustD","CustView")'">
                FORM-ALL &raquo;
            </button>

            @*<input type="submit" form="CustForm" />

                <input type="submit" form="CustForm2" />*@
        </p>


    </div>
</div>

<form id="CustForm" method="post" action="/CustView/SaveCustB">
    <legend>Customer Details</legend>
    <table>

        @foreach (var item in Model)
        {

            <tr>
                <td>
                    <label for="genre">CustID:</label>
                </td>
                <td>
                    <input type="text" name="1CustID" value="@Html.DisplayFor(modelItem => 
item.CustID)" readonly="readonly" />
                </td>
            </tr>
            <tr>
                <td>
                    <label for="genre">CustName:</label>
                </td>
                <td>
                    <input type="text" name="2CustName" value="@Html.DisplayFor(modelItem => 
item.CustName)" />
                </td>
            </tr>
            <td>
                <label for="genre">Cust Notes:</label>
            </td>
            <td>
                <input type="text" name="3CustNotes" value="@Html.DisplayFor(modelItem => 
 item.CustNotes)" />
            </td>
            <tr>
                <td></td>
                <td>
                    <input type="submit" name="action:Save1" value="Save" />
                </td>
                <td>
                </td>
            </tr>
        }
    </table>
</form>

<form id="CustForm2" method="post" action="/CustView/SaveCustC">
    <legend>Customer Details</legend>
    <table>

        @foreach (var item in Model)
        {

            <tr>
                <td>
                    <label for="genre">CustID:</label>
                </td>
                <td>
                    <input type="text" name="B1CustID" value="@Html.DisplayFor(modelItem => 
 item.CustID)" readonly="readonly" />
                </td>
            </tr>
            <tr>
                <td>
                    <label for="genre">CustName:</label>
                </td>
                <td>
                    <input type="text" name="B2CustName" value="@Html.DisplayFor(modelItem => 
 item.CustName)" />
                </td>
            </tr>
            <td>
                <label for="genre">Cust Notes:</label>
            </td>
            <td>
                <input type="text" name="B3CustNotes" value="@Html.DisplayFor(modelItem => 
 item.CustNotes)" />
            </td>
            <tr>
                <td></td>
                <td>
                    <input type="submit" name="action:Save1" value="Save" />
                </td>
                <td>
                </td>
            </tr>
        }
    </table>
</form>
</body>
</html>

You have to follow some steps that's will fulfill your requirement. 1->Create a common Model Cust include other form classes like (Form1 & Form2). 2->Use only one Form at page start if your form is submitting in one method or if multiple methods are required then use form-name='form-id' in each form fields and give ID to each form And on last form submit trigger others forms click events.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM