将参数传递给Action方法
本文关键字:方法 Action 参数传递 | 更新日期: 2023-09-27 17:57:56
我有一个类似的操作方法
public JsonResult Create(Product p, string extra)
视图绑定到@model Product
在通过ajax调用调用Create操作时,我从表单中获得ProductP值,但extra始终为null,尽管extra的形式与相同
<input type="text" name="extra" />
我也试过Request。Form["extra"]也为null。我缺少什么?如何在action方法中获取input〔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" />
}
你可以不引人注目地AJAX化这个表单,如下所示:
$('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;
});