7

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)

11
  • It would be very easy if you just serialize the form! Commented Aug 27, 2015 at 9:42
  • 1
    what about if you send data in POST method :) Commented Aug 27, 2015 at 9:43
  • 3
    instead of ViewMethodOfPayment[] vmop try putting List<ViewMethodOfPayment> vmop Commented Aug 27, 2015 at 9:44
  • 1
    @GuruprasadRao I'll try that first, actually, I'll respond with the result
    – Toza
    Commented Aug 27, 2015 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.. Commented Aug 27, 2015 at 9:56

2 Answers 2

3

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;
    }
}
4
  • 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.
    – Toza
    Commented Aug 27, 2015 at 10:03
  • 1
    Good point, the OP typically sends out a string, not an array since he uses JSON.stringify(vmopl).
    – Tasos K.
    Commented Aug 27, 2015 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!
    – Toza
    Commented Aug 27, 2015 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
    Commented Aug 27, 2015 at 10:42
0

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

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.