簡體   English   中英

c# asp.net mvc 一鍵提交來自多個 forms 的數據在一個視圖上

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

我有一個視圖,上面有 2 個 forms。 我希望在 forms 之外有 1 個按鈕,這些按鈕在一個 go 中提交來自兩個 forms 的數據。 我可以使用每個表單中的按鈕提交數據。 有沒有辦法按下兩個 forms 之外的按鈕,該按鈕可以訪問來自兩個 forms 的數據以發送到數據庫? 我的視圖代碼如下。 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. 請注意,IsPost 方法試圖將全局變量作為 forms 文本框之一,但它似乎不起作用,可能是因為 FORM-ALL 按鈕沒有發布帖子?

@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>

您必須按照一些步驟來滿足您的要求。 1->創建一個通用的 Model 客戶包括其他表單類,如(Form1 & Form2)。 2->如果您的表單以一種方法提交或需要多種方法,則在頁面開始時僅使用一個表單,然后在每個表單字段中使用 form-name='form-id' 並為每個表單提供 ID 並在最后一個表單提交觸發其他 forms 點擊事件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM