-1

How do I know what parameter to create when passing a javascript object.

Please consider

var all = [];
//loop through each instrument
for (var i = 0; i < 3; i++) {
    var txt = getThis(i);//int
    var detail =getThat(i);//string

    all.push({
        'this': txt,
        'that': detail
    });
  }  
    ajaxCall.getNow("myUrl", {
        all
    };

Where ajaxCall.getNow simply calls the typical $ajax with get.

The question is what parameter do I need in my MVC controller.

If I use object then it works but I'm not sure what I need to do to work with the object. As an object, it's fairly useless.

If I try tuple<in, string> it doesn't work

EG

public JsonResult MyFunction(Tuple<int,string,string,double,double,string,int> all)//this fails


public JsonResult MyFunction(object all)//this works but I only have an ojbect, I@d like to have something like the tuple to work with

So how do I know what type of parameter to set in my MVC controller?

MyDaftQuestions
  • 4,487
  • 17
  • 63
  • 120

1 Answers1

1

Your code will send data like this in your ajax call

{
    "all": [
        {
            "this": 0,
            "that": "some"
        },
        {
            "this": 1,
            "that": "some"
        },
        {
            "this": 2,
            "that": "some"
        }
    ]
}

You can create a C# representation of this data.

public class All
{
    public int @this { get; set; }  //because this is a reserved keyword
    public string that { get; set; }
}

public class RootObject
{
    public List<All> all { get; set; }
}

You can use the RootObject as your param

public ActionResult SomeActionName(RootObject model)
{

}

Remember, this is a C# keyword, so try to avoid that. May be you can replace it with myThis

Shyju
  • 214,206
  • 104
  • 411
  • 497
  • Ah, so I need a C# object which corrosponds to the javascript object I'm sending? – MyDaftQuestions Dec 30 '16 at 20:48
  • Yes. You need the C# object. Create those classes in your project and use that as the parameter. The model binder will be able to map the posted form data(from your ajax call) to the object of the `RootObject` – Shyju Dec 30 '16 at 20:51
  • When I do this, in my controller, RootObject shows the correct number of items (based upon the number of items in the javascript array) but all properties of my C# object are empty... So as per your example, the javascript passes 3 items. I can see RootObject has 3 items, but when I look at the each item, all parameters are null – MyDaftQuestions Jan 03 '17 at 18:25
  • since it is a complex object, you need to use `$.post` (which does a post). Here are some sample code http://stackoverflow.com/questions/20226169/how-to-pass-json-post-data-to-web-api-method-as-object/20226220#20226220 – Shyju Jan 03 '17 at 18:27
  • But if I do post, then the javascript post asyncronously I believe? I need to avoid it being asyncronous as I want to show a "spinner" whist it saves... – MyDaftQuestions Jan 03 '17 at 18:27
  • yes. jQuery has a method called $.post which does an asynchronous posting – Shyju Jan 03 '17 at 18:28
  • I need to **avoid** async – MyDaftQuestions Jan 03 '17 at 18:28
  • if you want to post data from js, you need to use ajax, else you need to write all messy code to set data to your form elements (with proper element names -so that model binding will work). and do a normal form submit. – Shyju Jan 03 '17 at 18:29
  • hmmm... that will involve a post back, meaning no spinner... I understand, thank you for your help – MyDaftQuestions Jan 03 '17 at 18:30