1

Trying to pass a dictionary from JS to MVC with no success. The data (reportIdAndStatus in mvc controller) is null.

I'm using angular $resource and MVC 3. Thanks.

Angular Controller:

var reportStatusdictionary = {};
//build a dictionary
            for (var x = 0; x < reports.length; x++) {
                reportStatusdictionary[reports[x].Id] = reports[x].StatusId
            }

reportsService.getReportStatusComment({ reportStatusdictionary: reportStatusdictionary }

Angular Resource Definition:

 getReportStatusComment:
        {
            method: 'GET',
            params: { reportIdAndStatus: '@reportIdAndStatus', command: 'GetReportStatusComment' },
            isArray: false
        }

MCV Controller:

    [HttpGet]
    public string GetReportStatusComment(Dictionary<Guid, int> reportIdAndStatus)

Request In Browser:

enter image description here

5
  • Try to send dictionary from server to client and capture the format and try to send the same. Commented May 21, 2014 at 12:59
  • Why you not using POST metod ? Should be easier Commented May 21, 2014 at 13:55
  • @user3314659 since I'm not changing any resources Commented May 21, 2014 at 14:22
  • OK so how can your url looks like ? Commented May 21, 2014 at 14:43
  • See the image I attached in the question (under "Request In Browser") Commented May 21, 2014 at 14:44

1 Answer 1

0

Instead of dictionary,I'd recommend creating a view model for simplicity and ease of debugging purposes. You can do as below:

  1. Create view model like below:

       public class ReportViewModel
       {
           public Guid Id{get;set;}
           public int StatusId{get;set;} 
       }
    
  2. Then your controller will look like

    [HttpGet]
    public string GetReportStatusComment(IEnumerable<ReportViewModel> reportViewModel)
    
  3. Then your get url will be

    http://localhost:53854/report/getreportstatuscomment?[0].id=guidValue1&[0].StatusId=1&[1].id=guidValue2&[1].StatusId=2

    You can easilty construct the url structure like this in your JavaScript Code. You can read more on model binding at @Darin Dimitrov's answer, Phil Haack's blog post and MSDN magazine article.

Update

If you don't want to build that url structure then you can send JSON to server.

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

1 Comment

Thanks but personally I tend to always use primitives \ built-in types when possible thus a view model option is an overhead for me. Regarding the MSDN article, it explains the how to build a costume value provider, which is again an overhead for the problem I presented. I predict the solution to be very simple, since I'm trying to do a very simple operation.

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.