简体   繁体   English

将参数传递给 Action 方法

[英]Passing a parameter to Action method

I have an action method like this我有这样的动作方法

public JsonResult Create(Product p, string extra)

The view is bound to @model Product视图绑定到@model Product

On calling Create action via ajax call, I am getting Product P values from the form but extra is always null, although extra is in the same form在通过 ajax 调用调用创建操作时,我从表单中获取产品 P 值,但额外的始终是 null,尽管额外的形式相同

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

I also tried Request.Form["extra"] it was null too.我也试过 Request.Form["extra"] 它也是 null 。 What I am missing?我错过了什么? how to get value of input[name=extra] in action method?如何在操作方法中获取 input[name=extra] 的值?

You didn't mention how are you calling this action (other than saying AJAX which obviously is not enough), so assuming you have an HTML form representing a product and an input field containing some extra value:你没有提到你是如何调用这个动作的(除了说 AJAX 这显然是不够的),所以假设你有一个代表产品的 HTML 表单和一个包含一些额外值的输入字段:

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

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

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

you could unobtrusively AJAXify this form like this:你可以像这样悄悄地 AJAXify 这个表单:

$('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;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM