如何解决Viewbag获得空值
我有两个Controller ResultController和HomeController。 从结果控制器的索引ActionResult中,我正在通过ViewBag将参数值传递给视图
public IActionResult Index(string listingType,string location,string viewType,[FromQuery] SearchModel searchModel)
{
SearchQuery model = new SearchQuery(searchModel,location,this.cacheService);
var locationList = this.searchFactory.GetSearchService(model.ListingType).GetSearchResults(model,listingType);
ViewBag.listingType = listingType;
ViewBag.location = location;
ViewBag.viewType = viewType;
ViewBag.searchModel = searchModel;
return View(locationList);
}
现在In View通过viewbag接收值,并将值传递给Homecontroller的PropertyDetails
@Html.ActionLink(linkText: "Comfortable Apartment in Palace","PropertyDetails","Home",new { ListingId = pl.ListingId,listingType= ViewBag.listingType,location= ViewBag.location,viewType= ViewBag.viewType,searchModel= ViewBag.searchModel },null)
在Homecontroller的PropertyDetails中
public IActionResult PropertyDetails(string listingType,[FromQuery] SearchModel searchModel,int ListingId)
{
SearchQuery model = new SearchQuery(searchModel,listingType);
var result = locationList.SalesList.FirstOrDefault(s=>s.ListingId==ListingId);
return View(result);
}
在PropertyDetails中,我获取了viewbag传递的所有参数值,但是searchmodel参数为null。有人可以告诉我为什么我在searchmodel中获取null 参数?还有传递值的替代方法吗?
Searchmodel类
public class SearchModel
{
public string PI { get; set; }
public string SIMP { get; set; }
public string L { get; set; }
public string S { get; set; }
public int[] LocationId;
}
解决方法
@{
var LocationId = (new int[] { 1,2,3 });
}
@Html.ActionLink("Comfortable Apartment in Palace","PropertyDetails","Home",new { ListingId = 1,listingType = 2,location = 3,viewType = 4,L = "L",PI = "PI",S = "S",SIMP = "SIMP",Locations = string.Join(",",LocationId) },null)
SearchModel类
public class SearchModel
{
public string PI { get; set; }
public string SIMP { get; set; }
public string L { get; set; }
public string S { get; set; }
public int[] LocationId;
public string Locations { get; set; }
}
正确:
new { L = "L",SIMP = "SIMP" }
错
new { searchModel= ViewBag.searchModel }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。