【发布时间】:2020-11-12 08:44:59
【问题描述】:
我有两个控制器 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);
}
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;
}
【问题讨论】:
标签: c# asp.net-mvc view