0

I’m new to MVC so I’m not sure if this is the correct way of doing this. What I’m trying to do is send some JSON data to my controller via ajax, then to my model, to eventually do something to it. The object I’m sending looks like this before I JSON it on the javascript side

bannerID:undefined
bannerMode:"WIDGET"
heightSettings:"0"
images:Array[2]
toggleArrows:"true"
toggleIndicators:"true"
transitionSpeed:""
widthSettings:"0"

When I JSON it looks like this, when I view it on my controller by replacing widget with string i get this.

 {"bannerMode":"WIDGET","transitionSpeed":"","toggleArrows":"true","toggleIndicators":"true","widthSettings":"0","heightSettings":"0","images":

 [{"id":0,"orderRank":"1","imageLink":"http://localhost:49999/img/admin/banner/example.jpg"},{"id":1,"orderRank":"2"}]}

In the parameters of the controller of my controller I have a widget model.

 public ActionResult Index(widget widgetData){
  return View("~/Views/widgets/_dynamicWidget.cshtml", widgetData);
 }

My widget model looks like this.

public class widget
 {
    public string bannerMode { get; set; }
    public int transitionSpeed { get; set; }
    public bool toggleArrows { get; set; }
    public bool toggleIndicators { get; set; }
    public int widthSettings = 20;
    private string lang;
    private List<WidgetList> images1;

    public widget(string lang, List<WidgetList> images1)
    {
        this.lang = lang;
        this.images1 = images1;
    }

    public int heightSettings{ get; set; }
    public List<ImageList> images { get; set; }
}

    public class ImageList
{
    public int id { get; set; }
    public int orderRank { get; set; }
    public string imageLink { get; set; }
}

I’m guessing the data should be set into the model ? But in my view I’m getting a null error when I try to show this data.

4
  • check your ajax call url action and controller correct or not? Commented Jul 20, 2016 at 13:27
  • Not entirely sure what you are asking for - are you asking about converting the JSON to a C# class? - stackoverflow.com/questions/2246694/… Commented Jul 20, 2016 at 13:29
  • Thanks for your response, the ajax is definitely reaching the controller. When I change the parameters of the controller to "string:widgetData" I can view or the JSON. Commented Jul 20, 2016 at 13:29
  • 1
    Is that a GET method or a POST method (it will not work with a GET). And your widget class has no parameterless constructor so it would throw an exception anyway Commented Jul 20, 2016 at 13:30

1 Answer 1

1

Your widget class does not have a public parameterless constructor, so I'm guessing the model binder is failing there.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks what would you put in the parameters the json data?
No, you want no parameters on your constructor (or 2 constructors - one without parameters for the model binder and 1 for you to use when you create a new widget in your code).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.