如何使用jQuery Form Plugin向我的表单发送额外数据?

时间:2022-04-06 07:15:26

In my .NET MVC4/razor app I have a form, and I want to include other data that is not found in the form as part of the request to my controller method. However, it doesn't appear to be adding this extra data. I'm running into a model binding error: The parameters dictionary contains a null entry for parameter 'B' of non-nullable type 'System.Boolean'.. basically, it's not adding the extra data to the request. Any clue what I'm doing wrong?

在我的.NET MVC4 / razor应用程序中,我有一个表单,我想在表单中包含其他数据,作为对我的控制器方法的请求的一部分。但是,它似乎没有添加此额外数据。我遇到了模型绑定错误:参数字典包含非可空类型'System.Boolean'的参数'B'的空条目。基本上,它不会将额外数据添加到请求中。我有什么问题吗?

Html:

@using (Html.BeginForm("MyAction", "MyController", FormMethod.Post, new { name = "myForm", id = "myForm" }))
{
    //blah
}

Controller:

public ActionResult MyAction(MyViewModel viewModel, string A, bool B)
{
    //do stuff
}

Javascript... I've tried this a couple of ways and cant get it to work.

Javascript ...我已经尝试了几种方法,但无法让它工作。

beforeSerialize:

$('#myForm').ajaxForm({
    beforeSerialize: function (form, options) {
        options.data = {
            A: "test",
            B: true
        };
    },
    success: function (result) {
        //do stuff
    }
});

beforeSubmit:

$('#myForm').ajaxForm({
    beforeSubmit: function (formData, formObject, formOptions) {
        formData.push({A: "test", B: true });
    },
    success: function (result) {
        //do stuff
    }
});

data:

$('#myForm').ajaxForm({
    data: {
        A: "test",
        B: true
    },
    success: function (result) {
        //do stuff
    }
});

1 个解决方案

#1


0  

I solved this. The problem is that the div containing my form was getting re-rendered on a different ajax event and blowing the binding magic that happens on page load.

我解决了这个问题问题是包含我的表单的div正在重新呈现在不同的ajax事件上,并吹响页面加载时发生的绑定魔法。

#1


0  

I solved this. The problem is that the div containing my form was getting re-rendered on a different ajax event and blowing the binding magic that happens on page load.

我解决了这个问题问题是包含我的表单的div正在重新呈现在不同的ajax事件上,并吹响页面加载时发生的绑定魔法。