3

I am using MVC3, Razor, C#, JQuery and AJAX.

I am using an Ajax call to postback a form to the server. I get all the form element values being passed back to the controller in:

    [HttpPost]
    public ActionResult Edit(Product myProduct, string actionType)

    if (actionType == "Save")
       save....

And in the View I have:

@using (Html.BeginForm("Edit", "RA", FormMethod.Post, new { @class = "editForm", @id = "frmEdit" }))

Form Elements:

<td>
@Html.HiddenFor(p=>p.Id) 
<button type="submit" name="actionType" value="Save" >Save</button>
</td>
<td>@Html.EditFor(p=>p.Name)</td>

Some Ajax:

    $('.editForm').on('submit', function () {
    $.ajax({
        url: this.action,
        type: this.method,
        data: $('#frmEdit').serialize(),          
        context: $('button', this).closest('tr'),
        success: function (result) {
            $(this).html(result);
        }
    });
    return false;
});

Now I think the problem line is since I have seen quite a few posts about problem with JQuery and submitting button values:

        data: $('#frmEdit').serialize(), 

But I cannot get the button to submit an actionType of "Save". I just get null.

Thoughts greatly appreciated.

Thanks.

UPDATE:

My code seems to interfere with my JQuery listener ?? My code is:

<input type="submit" id="btn" name="btn" value="Save" onclick="document.getElementById('actionType').value = 'Save';"/>
4

1 回答 1

1

文档中:

由于未使用按钮提交表单,因此没有序列化提交按钮值。

但是,您可以手动添加它:

data: $('#frmEdit').serialize() + '&actionType=Save',

或者

data: $('#frmEdit').serialize() 
    + '&'
    + encodeURIComponent(button.name)
    + '='
    + encodeURIComponent(button.value),

DOM 元素button在哪里。<button>

于 2013-05-09T23:53:07.787 回答