【问题标题】:Viewbag getting null valueViewbag获取空值
【发布时间】: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


    【解决方案1】:
    @{
        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 }
    

    【讨论】:

    • 这是一个代码唯一的答案,请描述是什么使这个工作和 OP 做错了什么。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-07
    • 2015-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-10
    • 1970-01-01
    相关资源
    最近更新 更多