且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

ASP.NET MVC传递JSON从控制器查看

更新时间:2023-02-25 19:18:50

希望这可以给你它是如何实际工作的一些想法

Hope this can give you some idea on how it actually work

回报的ActionResult的一些例子来查看

Some example of return as actionresult to view

控制器

public ActionResult something(string userGuid)
{
    var p = GetUserProducts(userGuid);
    return view(p); //you can return as partial view  (return PartialView("your partial view name", p));
}

查看

@model IEnumerable<productDetails>


 foreach (var item in Model)
{
   @Html.DisplayFor(model => item.Id)
   //and so on
}

JsonResult

JsonResult

回报JSON的一些例子来查看

Some example of return as json to view

控制器

   [httpPost]
    public JsonResult something(string userGuid)
    {
        var p = GetUserProducts(userGuid);
        return Json(p, JsonRequestBehavior.AllowGet);
    }

使用Ajax调用

$.post( "../something", {userGuid: "foo"}, function( data ) {
  console.log(data)
});