【发布时间】:2017-01-27 13:06:42
【问题描述】:
我有一个动作
[HttpGet]
public ActionResult Index(MyObject obj)
{
return View(obj);
}
还有一个对象:
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
public class MyObject
{
[JsonProperty(PropertyName = "someString")]
public String SomeString
{
get { return _someString; }
set { _someString = value; }
}
[JsonProperty(PropertyName = "someDictionary")]
public IDictionary SomeDictionary
{
get { return _someDictionary; }
set { _someDictionary = value; }
}
public MyObject()
{
}
private String _someString;
private IDictionary _someDictionary;
}
我想通过 url 将此对象传递给操作。所以我创建了网址:
String url = controller.Url.Action("Index", "SomeController");
url += "?obj=" + JsonConvert.SerializeObject(myObjectInstance);
它为我创建了一个带有 json 的 url,但是当我使用它时 - url 中的对象为空。
谁能帮帮我?
谢谢!
更新: 分享我的 MyObject 实例作为对 cmets 的反应:
MyObject myObjectInstance = new MyObject ();
myObjectInstance.SomeString = "Hello";
myObjectInstance.SomeDictionary = new Dictionary<String, Object>
{
{"firstKey","value"},
{"secondValue",5}
}
【问题讨论】:
-
如果您尝试通过重定向传递对象,这可能对How do I include a model with a RedirectToAction? 有所帮助
-
不能在 URL.Action 方法中直接使用对象本身吗?即
string url = controller.Url.Action("Index", "SomeController", myObjectInstance);msdn.microsoft.com/en-us/library/… -
艾迪森。太近了,对象是绑定的,但是没有IDictionary对象
-
Shyju,谢谢。我想到了 PRG Pattern 之类的东西,因为在我的情况下我不能使用 TempData
-
如果你想绑定到
Dictionary,那么数据需要采用正确的格式——参考this answer——你甚至没有指出你的myObjectInstance数据是什么!
标签: c# asp.net json asp.net-mvc