我有两个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, string location, string viewType, [FromQuery] SearchModel searchModel, int ListingId)
{
SearchQuery model = new SearchQuery(searchModel, location, this.cacheService);
var locationList = this.searchFactory.GetSearchService(model.ListingType).GetSearchResults(model, 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;
}
答案 0 :(得分:0)
@{
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", PI = "PI", S = "S", SIMP = "SIMP" }
错
new { searchModel= ViewBag.searchModel }