2

I want to pass javascript object from view to controller:

var test = { name: "Sydney", country: "AU" };
var tt = JSON.stringify(test);

$.ajax({
  url: '@Url.Action("getFeeList", "FeeControl")',
  type: "POST",
  data: { test: test },
  dataType: "html",
  success: function (FeeListResp, textStatus, jqXHR) {

  },
  error: function (jqXHR, textStatus, errorThrown) {
  },
  complete: function () {
  }
});

In controller:

public class Addr
{
  public string name { get; set; }
  public string country { get; set; }
}

[HttpPost]
public string getFeeList(Addr test)
{
  string nm = test.name;

  string j = new LoadItem(loadItemUnitWork, nm, 30, true, 0).GetItem(); 
  return j;
}

if I pass data: { test: test }, in ajax, the test.name is null in controller. if I pass data: { test: tt}, in ajax, then the test is null in controller.

How to fix the problem?

Thank you.

2
  • Just for more information, what MVC framework are you using? Commented Sep 18, 2013 at 5:14
  • Did you try data: test? Commented Sep 18, 2013 at 13:14

1 Answer 1

1
var test = { name: "Sydney", country: "AU" };
        var tt = ko.toJS(test);

        $.ajax({
            url: '@Url.Action("getFeeList", "FeeControl")',
            type: "POST",
            data: tt,               
        });

I have used knockout here.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.