【问题标题】:How would I pass a json object from a ActionMethod as a parameter to a different Action Method in MVC?如何将 json 对象作为参数从 Action Method 传递给 MVC 中的不同 Action Method?
【发布时间】:2015-11-02 10:53:50
【问题描述】:

在我的项目中,我的控制器中有两个 ActionResult 方法。第一个 ActionResult 方法是从 Ajax 调用中检索 Json。

ActionResult Method1 是这样的:....

public ActionResult VehicleModel(int id)
{
    var vehicle = myService.VehicleModel(id);
    myserviceService.Close();
    return Json(new { model = vehicle }, JsonRequestBehavior.AllowGet);
}

ActionResult 方法2

public ActionResult Vehicles(string id,  string vehicle)
{
    var _cat = _catalogue.Data.FirstOrDefault(x => x.manufacturerId == id && x.Vehicle == vehicle);
    ViewData["id"] = id;
    return PartialView("_vehicle", _cat);
}

现在..我可以在方法 2 中访问方法 1 吗?我要做的是获取方法(车辆)中返回的 json,并在方法 2 中作为参数传递。我不太确定我将如何实现这一目标。请帮助。

谢谢

【问题讨论】:

    标签: json asp.net-mvc


    【解决方案1】:

    希望我理解正确。

    如果您希望VehicleModel 将数据发送到Vehicles 并从那里继续,您应该使用这个:

    var jsonVehicle = Json(vehicle); //or whatever way you want to serialize your object to json
    return RedirectToAction("Vehicles", new { id = this.id, vehicle = jsonVehicle});
    

    这会将您的操作重定向到Vehicles,并且还会绑定路由值。

    有关 RedirectToAction 的更多信息,read this

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-16
      • 2016-09-09
      • 1970-01-01
      • 1970-01-01
      • 2014-02-03
      相关资源
      最近更新 更多