peter
peter

Reputation: 1049

How to pass javascript object from view to controller?

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.

Upvotes: 2

Views: 3087

Answers (1)

Preeti Singh
Preeti Singh

Reputation: 56

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.

Upvotes: 1

Related Questions