如何将作为参数的ActionMethod中的json对象传递给MVC中的其他Action方法?

时间:2015-11-02 10:53:50

标签: json asp.net-mvc

在我的项目中,我的Controller中有两个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 Method2

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并在Method2中作为参数传递。我不太确定我将如何实现这一点..请帮助。

谢谢

1 个答案:

答案 0 :(得分: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