繁体   English   中英

如何在@Url.Action 中将 json 对象发送到 C# 中的操作

[英]How to send json object to action in C# in @Url.Action

嗨,我正在尝试向我的操作发送一个 json 对象,但我无法发送它。 谁能帮我解决这个问题。 它驻留在其他一些控制器上

var pData = { id: null, StateCity: statecityName, Zip: zipcode, MaxDistance: maxdist, PhyName: phyName, ClickCnt: 0 };

window.location.href = '@Url.Action("Index", "Different", new { @pData=pData})';

这里pData说没有找到。

我的动作DifferentController

public ActionResult Index(PData pData )
{
//some work
return view();
}

如果您找不到发送 Json 对象的解决方案,这里是一个使用查询字符串的解决方案,它可以为您提供页面重定向

var pData = '?StateCity='+ statecityName + '&Zip='+ zipcode + '&MaxDistance=' + maxdist + '&PhyName=' + phyName;
window.location.href = '@Url.Action("Index", "Different")' + pData;

并且在您的控制器操作方法中需要添加所有必需的参数而不是复杂的对象来绑定

[HttpGet]
public ActionResult Index(string stateCity, string zip, int maxDistance, string phyName)
{
    //work with your parameters
    return view();
}

根据需要在查询字符串和操作方法参数列表中添加或删除参数。 这应该非常适合你。

暂无
暂无

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

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