Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

So getting the objects I need in JS, I did:

$('.combine-payment-input').each(function (index, value) {
    if (parseFloat(value.value) > 0) {
       if (methodOfPayment == -1) {
            methodOfPayment = value.dataset.method;
       }
       else {
           methodOfPayment = 0;
       }
       vmopl.push({
            id: value.dataset.method,
            name: $('label[for="' + value.id + '"]').html(),
            inUse: 'True',
            ammount: value.value
       });
    }
});

If I console.log vmopl in the end, I'll get something like

[Object { id="2",  name="Card",  inUse="True",  ammount="500"}, 
    Object { id="1",  name="Cash",  inUse="True",  ammount="250"}]

Now if I try to send this to AJAX this up using

$.get('/reports/savebill/' + methodOfPayment + '?vmop=' + JSON.stringify(vmopl), function (data) {
    if (data == 'True') {
        location.href = '/order/neworder/';
    } else {
        alert("Unsuccessful!");
    }
});

A controller action Should pick vmop up, the controller looks like so:

public bool SaveBill(int id, ViewMethodOfPayment[] vmop) { 
    //lots of code... 
}

But when I put a breakpoint, I always see vmop as null, even when I pass it to another object (var temp = vmop;).

ViewMethodOfPayment is a simple model class:

public class ViewMethodOfPayment
{
    public long Id { get; set; }
    public string Name { get; set; }
    public bool InUse { get; set; }
    public double Ammount { get; set; }
}

If I missed any info, or if it's unclear what I want to do/expect, please leave a comment, I'll answer as soon as I can!

Thanks for reading!

edit: changed the first block of code (line: 9, because I included a code that will bring a JavaScript error)

share|improve this question
    
It would be very easy if you just serialize the form! – Dhaval Marthak Aug 27 '15 at 9:42
1  
what about if you send data in POST method :) – Muhammad Usman Aug 27 '15 at 9:43
3  
instead of ViewMethodOfPayment[] vmop try putting List<ViewMethodOfPayment> vmop – Guruprasad Rao Aug 27 '15 at 9:44
1  
@GuruprasadRao I'll try that first, actually, I'll respond with the result – NemanjaT Aug 27 '15 at 9:46
1  
I feel your model is not getting the values properly because the properties inside it are case sensitive. So either while pushing to vmop assign it as it is in model like id should be Id.. Try once.. – Guruprasad Rao Aug 27 '15 at 9:56
up vote 2 down vote accepted

What I currently use:

Javascript sends the data via JSON.stringify, like you do.

C#:

public ActionResult AjaxDoSomething(string vmop)
{
    var jss = new JavaScriptSerializer();
    try
    {
        var parameter = jss.Deserialize<ViewMethodOfPayment []>(vmop);
        //Do something with this data and return the desired result
        return Json(result, JsonRequestBehavior.AllowGet);
    }
    catch
    {
        return null;
    }
}
share|improve this answer
    
I have thought of that, but purely because of that extra step (performance wise, this web service won't go on a super fast server), I'm keeping it as a last resort. Edit: I'm reading the edit now, I'll try that next, might take me a few. – NemanjaT Aug 27 '15 at 10:03
1  
Good point, the OP typically sends out a string, not an array since he uses JSON.stringify(vmopl). – Tasos K. Aug 27 '15 at 10:03
1  
I'm not sure how clear it could be from a comment like this, but it pretty much works! (thank you very much!). @TasosK. gave me a clarification that it's a string already, so I took it as such (such as before you edited the parameter in the answer) and turned it into an objects as you wrote it. Thanks you again! – NemanjaT Aug 27 '15 at 10:15
1  
Thanks for the comment about the wrong parameter type in my edited example. That was a blunder on my part and is fixed now. – MilConDoin Aug 27 '15 at 10:42

try putting [FromUri] before ViewMethodOfPayment[] vmop as below

public bool SaveBill(int id,[FromUri] ViewMethodOfPayment[] vmop) { 
//lots of code... 
}
share|improve this answer
    
Will try that in a bit. – NemanjaT Aug 27 '15 at 9:51
    
[FromUri] doesn't belong to any known annotation library? where do you implement it from? – NemanjaT Aug 27 '15 at 9:52
    
if your controller inherits from ApiController you can use [FromUri] – VuongNQ Aug 27 '15 at 9:57
    
Problem is, since this is a team project, making it extend ApiController is out of the question – NemanjaT Aug 27 '15 at 10:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.