Cap*_*nge 2 c# asp.net jquery razor asp.net-mvc-3
我在ASP.NET MVC3网站上的视图中有以下ajax函数.此功能从页面顶部的列表中调用,效果很好.但我需要来自查询字符串的另一个值,需要传递给控制器函数.我如何实现这一目标?
jQuery函数
function ShowDeals(itemNo) {
//get the vendor
var dialogDIV = $('<div></div>');
$.ajax(
{
url: '/Deal/Index',
type: "POST",
data: ({ toets: itemNo }),
dataType:'html',
success: function (result) {
$(dialogDIV).html(result);
$(dialogDIV).dialog({
position : [,100],
error: function (req, status, error) {
alert(error);
}
});
},
error: function (req, status, error) {
alert(error);
}
})
return false;
}
Run Code Online (Sandbox Code Playgroud)
控制器动作
public ActionResult Index(int toets,string vendorNo)
{
string temp = toets.ToString();
string tempdd = Request.QueryString["vendorNo"];
//return Content("1212121212");
return PartialView();
}
Run Code Online (Sandbox Code Playgroud)
toets参数从ajax函数传递,但我现在需要Querystring中的vendorNo.
谢谢
编辑:我知道我可以添加一个javascript函数来从查询字符串中获取值,但这是最好的方法吗?
您可以像传入toetsparam 一样传递它:
var vendor = $("#TheElementThatHoldsYourVendor").text();
$.ajax(
{
url: '/Deal/Index',
type: "POST",
data: ({ toets: itemNo, vendorNo: vendor }),
....
});
Run Code Online (Sandbox Code Playgroud)
然后你将它作为第二个参数,你不必访问它QueryString来获取它.
编辑
要使用javascript获取url参数,您可以使用此方法(从此处)
function getUrlVars()
{
var vars = [], hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for(var i = 0; i < hashes.length; i++)
{
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
return vars;
}
Run Code Online (Sandbox Code Playgroud)
然后像这样调用它:
$.ajax({
...
data: { toets: itemNo, vendorNo: getUrlVars()['vendorNo'] }
...
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7711 次 |
| 最近记录: |