我有两个控制器ResultController和HomeController。从Result Controller的Index 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);
}
Now 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吗参数还有其他传递价值的方法吗?
搜索模型类
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", PI = "PI", S = "S", SIMP = "SIMP" }
错误的
new { searchModel= ViewBag.searchModel }