从 MVC 中的按钮单击外部窗体发送文本框值

本文关键字:文本 窗体 外部 MVC 按钮 单击 | 更新日期: 2023-09-27 17:56:05

我有一个视图,我得到了我的Ajax.BeginForm,它应该提交。但是我有一个"保存图标",当我按下该按钮时,我需要表单内部的值。表单提交显示计算,保存图标假设保存所有计算。

但是当我按下保存图标(使用 Url.Action("SaveExcel"、"Save")时,我无法从文本框中获取值。我该怎么做?

我的网页:

        <a href="@Url.Action("SaveExcel", "Save")" title="Save"><img src="../../Images/glyphicons_446_floppy_save.png" alt="save" id="saveIcon"></a>
<!-- Contains forms and input for annuity calculation -->
<div class="calcInput" id="calcInput">
@using (Ajax.BeginForm("ShowDetailAnnuity", "Calculation", new AjaxOptions
{
    InsertionMode = InsertionMode.Replace,
    UpdateTargetId = "CalcDetail",
    LoadingElementId = "Loader",
}))
{
   <div class="calculateBox">
        <label for="calcOption">Choose value to calculate:</label>
        <select id="calcOption" name="calcOption" title="Choose what value you want to calculate">
            <option value="PMT" id="PMT" name="PMT">Payment (PMT)</option>
            <option value="I" id="I" name="I">Interest (I)</option>
            <option value="FV" id="FV" name="FV">Future value (FV)</option>
            <option value="PV" id="PV" name="PV">Present value (PV)</option>
        </select>
    </div>
    <div class="calculateBox" background-color="#777777">
        @Html.Label("Present value (PV)", new { @id = "pvLabel" })
        @Html.TextBox("PresentValue", null, new { @class = "PresentValue" })
        @Html.Label("Future value (FV)", new { @id = "fvLabel" })
        @Html.TextBox("FutureValue", null, new { @class = "FutureValue" })
        @Html.Label("Interest rate", new { @id = "intRateLabel" })
        @Html.TextBox("InterestRate", null, new { @class = "InterestRate" }) <br /> <br />
        <input type="radio" name="advanceOrArrears" id="inAdvance" value="inAdvance" /> In advance<br />
        <input type="radio" name="advanceOrArrears" id="inArrears" value="inArrears" /> In arrears
    </div>
    <div class="calculateBox">
        <label for="startDate">Start date:</label>
        <input type="date" id="StartDate" name="startdate" title="Choose start date for your calculation" /><br />
        @Html.Label("Payment frequency")
        <select id="PmtFreq" name="PmtFreq" title="Choose the payment frequency, for example: Every month">
            <option value="Monthly">Monthly</option>
            <option value="Quarterly">Quarterly</option>
        </select><br /><br />
        @Html.Label("No of payment periods")
        @Html.TextBox("PaymentPeriods")
        @Html.Label("Date time convention")
        <select id="DTC" name="DTC" title="Choose your Date time convention">
            <option value="360/360">360/360</option>
            <option value="365/365">365/365</option>
        </select><br /><br />
        <input type="submit" id="CalcBtn" class="calcBtn" name="SubmitBtn" value="Calculate" title="Calculate your calculation" />
    </div>
}
</div>

我的控制器:

public class SaveController : Controller
{
    public ActionResult SaveExcel(string PresentValue) //Can't get PresentValue here..
    {
        return View();
    }
}

我尝试使用文本框的名称作为控制器中的参数,我也尝试过 Request.Form["PresentValue"],但这些都不起作用

从 MVC 中的按钮单击外部窗体发送文本框值

听起来你需要两个提交按钮。

尝试将保存按钮转换为表单中的提交按钮,以便:

<input type="submit" name="SubmitButton" value="Calculate"/>
<input type="submit" name="SubmitButton" value="Save"/>

然后在控制器中,通过如下所示来区分按下的按钮:

public ActionResult SaveExcel(string PresentValue, string SubmitButton) 
{
    if(SubmitButton == "Save") .... <save code>
    if(SubmitButton == "Calculate") .... <calc code>
    return View();
}