How to pass json object from Javascript to asp.net mvc controller

I just started MVC, so I have a lot of confusion. whenever we call any function on the jquery server side in an asp.net webform project, this method must be static and be decorated with the webmethod attribute. so I want to know that the same rule applies in the case of mvc.

I have code for this, but I have not tested it.

client side method

function getTradeContribs(id,pfid, nodedates) {

    var data = {};
    data.portfolioId = pfid;
    data.nodedates = nodedates;

    $.ajax({
            type: "POST",
            url: "/Portfolios/getTradeContribs/"+id,
            dataType: "json",
            data: JSON.stringify(data),
            contentType: "application/json; charset=utf-8",
            success: parseTradeContribs,
            error: function (error) {
                    alert("failed in opening XML file !!!");
            }
    });
   }

server side method

public string getTradeContribs(int id,string portfolioId, string nodedates)
{
    string jsonTest = @" {""nodedates"": ""date"":""01/01/2012""}";
    return jsonTest;
}

from the above code I have few questions 1) how many types of controller methods exist in mvc 2) url: "/ Portfolios / getTradeContribs" , which URL. Are portfolios the name of the controller and getTradeContribs the name of the action? if not, then getTradeContribs is which method.

3) getTradeContribs ActionResult, 4) ActionResult? 5) id json. ?

, , mvc

+5
2

, - jquery webform asp.net, webmethod. , mvc.

, . ASP.NET MVC - . ASP.NET MVC , (, ActionResult). , JSON, , JsonResult :

public ActionResult GetTradeContribs(int id, string portfolioId, string nodedates)
{
    var model = new 
    {
        nodedates = "foo",
        date = DateTime.Now
    };
    return Json(model, JsonRequestBehavior.AllowGet);
}

:

public class TradeContribsRequestViewModel
{
    public int Id { get; set; }
    public string PortfolioId { get; set; }
    public string NodeDates { get; set; }
}

public class TradeContribsViewModel
{
    public string NodeDates { get; set; }
    public DateTime Date { get; set; }
}

:

public ActionResult GetTradeContribs(TradeContribsRequestViewModel request)
{
    var model = new TradeContribsViewModel
    {
        NodeDates = "foo",
        Date = DateTime.Now
    };
    return Json(model, JsonRequestBehavior.AllowGet);
}

ASP.NET MVC JSON . ajax:

$.ajax({
    url: '@Url.Action("GetTradeContribs", "Portfolios")',
    type: 'POST',
    data: { 
        id: 123, 
        portfolioId: 'some id', 
        nodedates: 'some node dates' 
    },
    success: function(result) {
        // You could directly use the properties of the result object here
        // like for example: alert(result.nodedates);
    }
});

JSON, getTradeContribs. , . , , :

  • , ActionResult
  • ,
+7

:

  • mvc - , . , URL, . RouteConfig Global.asax ASP.net MVC.
  • url: "/Portfolios/getTradeContribs", URL- - URL-. MVC URL- ( ) . asp.net mvc MVC.
  • getTradeContribs ActionResult why - ActionResult Controller. , MVC- - . ViewResult ( ), JsonResult ( JSON) .. JsonResult. returing , script JSON, JsonResult, script JSON .
  • ActionResult - ActionResult , Controller (HTML, JSON, XML ..). .
  • id json. . ASP.Net MVC Model Binder, , , . Modelbinder, , .

, . /. , . .

+2

All Articles