加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 使用RedirectToAction传递模型和参数

发布时间:2020-12-15 18:59:02 所属栏目:asp.Net 来源:网络整理
导读:我想将一个字符串和一个模型(对象)发送给另一个动作. var hSM = new HotelSearchModel();hSM.CityID = CityID;hSM.StartAt = StartAt;hSM.EndAt = EndAt;hSM.AdultCount = AdultCount;hSM.ChildCount = ChildCount;return RedirectToAction("Search",new { c
我想将一个字符串和一个模型(对象)发送给另一个动作.
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 });

当我使用new关键字时,它会发送null对象,尽管我设置了对象hSm属性.

这是我的搜索操作:

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

解决方法

您无法使用RedirectAction发送数据.
那是因为你正在进行301重定向,然后回到客户端.

你需要的是将它保存在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");

之后,您可以从TempData中再次检索:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读