传递模型和参数与RedirectToAction [英] passing model and parameter with RedirectToAction

查看:185
本文介绍了传递模型和参数与RedirectToAction的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一个字符串和一个模式(对象)发送到另一个动作。

  VAR HSM =新HotelSearchModel();
hSM.CityID = CityID;
hSM.StartAt = StartAt;
hSM.EndAt = ENDAT;
hSM.AdultCount = AdultCount;
hSM.ChildCount = ChildCount;返回RedirectToAction(搜索,新的{文化=文化,hotelSearchModel = HSM});

当我用它发送对象关键字,虽然我设置的对象 HSM 属性。

这是我的搜索动作:

 公众的ActionResult搜索(串文化,HotelSearchModel hotelSearchModel)
{
    // ...
}


解决方案

您不能用 RedirectAction 发送数据。
那是因为你正在做一个 301 重定向并返回到客户端。

你需要什么是它保存在TempData的:

  VAR HSM =新HotelSearchModel();
hSM.CityID = CityID;
hSM.StartAt = StartAt;
hSM.EndAt = ENDAT;
hSM.AdultCount = AdultCount;
hSM.ChildCount = ChildCount;
TempData的[MyObj中] = {新的文化=文化,hotelSearchModel = HSM};返回RedirectToAction(搜索);

之后,你可以从TempData的再次检索:

 公众的ActionResult搜索(串文化,HotelSearchModel hotelSearchModel)
{
    VAR OBJ = TempData的[MyObj中];
    hotelSearchModel = obj.hotelSearchModel;
    文化= obj.culture;
}

I want to send a string and a model (object) to another action.

var hSM = new HotelSearchModel();
hSM.CityID = CityID;
hSM.StartAt = StartAt;
hSM.EndAt = EndAt;
hSM.AdultCount = AdultCount;
hSM.ChildCount = ChildCount;

return RedirectToAction("Search", new { culture = culture, hotelSearchModel = hSM });

When I use the new keyword it sends null object, although I set the objects hSm property.

This is my Search action :

public ActionResult Search(string culture, HotelSearchModel hotelSearchModel)
{ 
    // ...
}

解决方案

You can't send data with a RedirectAction. That's because you're doing a 301 redirection and that goes back to the client.

What you need to is save it in TempData:

var hSM = new HotelSearchModel();
hSM.CityID = CityID;
hSM.StartAt = StartAt;
hSM.EndAt = EndAt;
hSM.AdultCount = AdultCount;
hSM.ChildCount=ChildCount;
TempData["myObj"] = new { culture = culture,hotelSearchModel = hSM };

return RedirectToAction("Search");

After that you can retrieve again from the TempData:

public ActionResult Search(string culture, HotelSearchModel hotelSearchModel)
{
    var obj = TempData["myObj"];
    hotelSearchModel = obj.hotelSearchModel;
    culture = obj.culture;
}

这篇关于传递模型和参数与RedirectToAction的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆