为什么我的onClick()按钮在控制器中调用HttpPost方法?

时间:2018-09-17 13:46:20

标签: asp.net asp.net-mvc-4

我有一个按钮,该按钮可以onClick()执行JS脚本,但是由于某种原因,它会在控制器内继续执行我的SubmitForm()Action方法,而我不明白为什么

  @using (Html.BeginForm("SubmitForm", "Home", FormMethod.Post,

                new
                {
                    traName = Request.Form["nameOfTra"],
                    amount = Request.Form["amountRequested"],
                    memberName = Request.Form["commiteeMember"],
                    date = Request.Form["agmDate"],
                    signed = Request.Form["signed"],
                    dated = Request.Form["dated"],
                    numberOfRows =  Request.Form["numberOfRows"]

}))
{
<h1 style="text-align: center;"> TRA grant application </h1>
<h4 style="text-align: center;">This is the TRA Grant form for the association named below who agree to use these funds to cover the cost of administration of the TRA</h4>
<p>
    <label for="nameOfTralbl">Name of TRA:</label>
    <input type="text" name="nameOfTra" value="" />
</p>
<h4> List of items the money will be spent on</h4>

<table id="traTable">
    <tr>
        <td>Description of Items</td>
        <td>Quantity</td>
        <td>Cost</td>
    </tr>
    <tr>
        <td><input type='text' size="30" /></td>
        <td><input type='text' size="30" /></td>
        <td><input type='text' size="30" /></td>
    </tr>
</table>
<br />
<button onclick="addRow()">Add Item</button>
<input type="hidden" id="rows" value="1" name="numberOfRows" />

<script>
    function addRow() {
        var table = document.getElementById("traTable");
        var row = table.insertRow(-1);
        var cell1 = row.insertCell(0);
        var cell2 = row.insertCell(1);
        var cell3 = row.insertCell(2);

        cell1.innerHTML = "<input type='text' size='30' id='cell1_" + $('#rows').val() + "'/>";
        cell2.innerHTML = "<input type='text' size='30' id='cell2_" + $('#rows').val() + "'/>";
        cell3.innerHTML = "<input type='text' size='30' id='cell3_" + $('#rows').val() + "'/>";
        $('#rows').val(parseInt($('#rows').val()) + 1)

    }
</script>

 public ActionResult SubmitForm(string traName,  float? amount,
            string memberName, string date, string signed, string dated, int? numberOfRows, HttpPostedFileBase file, HttpPostedFileBase file2, HttpPostedFileBase file3){}

我不想执行的Submitform()操作中有一些额外的逻辑,我不明白为什么它会继续调用此操作,而只是调用脚本即可。

1 个答案:

答案 0 :(得分:0)

您没有在SubmitForm()中声明提交按钮。在var a: Int? @objc func myFunction(completion:@escaping (Bool) -> () ) { DispatchQueue.main.async { let b: Int = 3 a = b completion(true) } } override func viewDidLoad() { super.viewDidLoad() myFunction { (status) in if status { print(self.a!) } } } 内添加您的提交按钮:

FormBegin

@编辑:

更改:

<input type="submit" value="Submit Button" />

收件人:

<button onclick="addRow()">Add Item</button>