Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将参数传递给Action方法_C#_Asp.net Mvc 3 - Fatal编程技术网

C# 将参数传递给Action方法

C# 将参数传递给Action方法,c#,asp.net-mvc-3,C#,Asp.net Mvc 3,我有一个这样的行动方法 public JsonResult Create(Product p, string extra) 视图绑定到@model Product 在通过ajax调用调用Create action时,我从表单中获取产品p值,但extra始终为空,尽管extra在同一表单中 <input type="text" name="extra" /> 我还尝试了请求。表单[extra]也为空。我错过了什么?如何在action方法中获取输入[name=extra]的值?您没有

我有一个这样的行动方法

public JsonResult Create(Product p, string extra)
视图绑定到@model Product

在通过ajax调用调用Create action时,我从表单中获取产品p值,但extra始终为空,尽管extra在同一表单中

<input type="text" name="extra" />

我还尝试了请求。表单[extra]也为空。我错过了什么?如何在action方法中获取输入[name=extra]的值?

您没有提到如何调用此操作,只是说AJAX,这显然是不够的,因此假设您有一个表示产品的HTML表单和一个包含一些额外值的输入字段:

@model Product
@using (Html.BeginForm())
{
    @Html.EditorForModel()

    <input type="text" name="extra" value="some extra value" />

    <input type="submit" value="Create" />
}

@coure06,太好了,那么应该不会有任何问题,因为我刚刚测试了它,它工作得非常完美。谢谢。。。你总是乐于助人。你能为我推荐一些关于ASP.NETMVC的好书吗?@coure06,是一本好书。很快将从同一作者那里发布。
$('form').submit(function() {
    $.ajax({
        url: this.action,
        type: this.method,
        data: $(this).serialize(),
        success: function(result) {
            // TODO: handle the results of the AJAX call
        }
    });
    return false;
});