0

I have an action

[HttpGet]
public ActionResult Index(MyObject obj)
{
   return View(obj);
}

and an object:

 [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;
  }

I want to pass this object to action by url. So I create url:

String url =  controller.Url.Action("Index", "SomeController");
url += "?obj=" + JsonConvert.SerializeObject(myObjectInstance);

It's create me an url with json, but when I use it - the object in url is null.

Can anybody help me?

Thanks!

UPDATED: Share instance of my MyObject as reaction to comments:

MyObject myObjectInstance = new MyObject ();
myObjectInstance.SomeString = "Hello";
myObjectInstance.SomeDictionary = new Dictionary<String, Object> 
{
   {"firstKey","value"}, 
   {"secondValue",5}
}
gado
  • 33
  • 1
  • 7
  • This might help [How do I include a model with a RedirectToAction?](http://stackoverflow.com/questions/11209191/how-do-i-include-a-model-with-a-redirecttoaction/11209320#11209320) if you are trying to pass an object with a redirect – Shyju Jan 27 '17 at 13:09
  • can't you just use the object itself in the URL.Action method? i.e. `string url = controller.Url.Action("Index", "SomeController", myObjectInstance);` https://msdn.microsoft.com/en-us/library/system.web.mvc.urlhelper.action(v=vs.118).aspx#M:System.Web.Mvc.UrlHelper.Action%28System.String,System.String,System.Object%29 – ADyson Jan 27 '17 at 13:14
  • ADyson. it's so close, the object is binded, but without IDictionary object – gado Jan 27 '17 at 16:55
  • Shyju, thanks. I fhinked about something like PRG Pattern, because I can't use TempData in my case – gado Jan 27 '17 at 16:56
  • If you want to bind to a `Dictionary` then the data needs to be in the correct format - refer [this answer](http://stackoverflow.com/questions/5191303/asp-net-mvc-binding-to-a-dictionary) - you have not even indicated what your `myObjectInstance` data is! –  Jan 27 '17 at 22:07
  • My data is standart ,net dictionary, but I updated my post and I gus that make dictionary serialization manualy is very bad. – gado Jan 28 '17 at 09:03

2 Answers2

0

The modelbinder is capable of deserializing JSON. In other words, you don't need to do anything special. If you're request body consists of JSON (not a JSON string mind you, but an actual JSON object with a JSON mimetype), then as long as it can bind the data to the action parameter, it will. In other words, all you need to do is contruct one or more C# classes to represent the JSON object. For example, given a JSON object like:

{
    'foo': 'bar',
    'bar': 1
}

You'd need a class like:

public class MyClass
{
    public string Foo { get; set; }
    public int Bar { get; set; }
}

Then, this goes into your action signature:

public ActionResult MyAction(MyClass foo)

The modelbinder will new up an instance of MyClass and bind the JSON data onto the relevant properties.

Chris Pratt
  • 232,153
  • 36
  • 385
  • 444
-1

Other than the fact that you should never engage in such atrocious behavior (you should only pass the object's ID in the link)... technically, you could do it this way:

public ActionResult Index(string myObjectJson)
{
     var myObject = Json.Deserialize<MyObject>(myObjectJson);
Serj Sagan
  • 28,927
  • 17
  • 154
  • 183
  • Yeah, I know, I can do it, but it is not beautyful solution, but thanks for your answer – gado Jan 27 '17 at 16:47