【问题标题】:How can i pass model to view in asp.net core 5.0 mvc我如何通过模型在 asp.net core 5.0 mvc 中查看
【发布时间】:2021-06-29 03:44:42
【问题描述】:

我正在尝试将该数据传递给我的视图。我已经环顾四周我能做什么或我可以使用哪种方式或其他东西,但是当我调用 ViewBag.passData 时,我仍然收到 null 异常错误。

这是我的BaseController


 public class BaseController : Controller
    {
        public BaseViewModel baseViewModel;

        public readonly IUnitOfWork<SiteSetting> _unitOfWorkSiteSetting;
        public readonly IUnitOfWork<Category> _unitOfWorkCat;

        public BaseController(
            IUnitOfWork<SiteSetting> unitOfWorkSiteSetting,
            IUnitOfWork<Category> unitOfWorkCat)
        {
            _unitOfWorkSiteSetting = unitOfWorkSiteSetting;
            _unitOfWorkCat = unitOfWorkCat;

           baseViewModel = new BaseViewModel
            {
                SiteSettings = _unitOfWorkSiteSetting.RepositorySiteSettings.GetSiteSettingsByID(2),//1-Admin,2-Shopping,3-WebFair
                IsFeaturedCategories = _unitOfWorkCat.RepositoryCategory.IsFeaturedCategoriesAsNoTracing(10)
            };
            ViewBag.passData = baseViewModel;
        }
    }

你可以看到我在那个BaseController 中填满了我的BaseViewModel。 我正在尝试这样做,因为我在每一页中都调用了这些数据。 当我在我看来调用ViewBag.passData 时,我得到了空异常错误。

这是我的看法:

@model IndexViewModel;
@using Microsoft.Extensions.Configuration
@inject IConfiguration Configuration
@{ ViewData["Title"] = "Anasayfa";
    Layout = null;
    BaseViewModel baseViewModel = new BaseViewModel();
    baseViewModel = ViewBag.passData; ***//The point where i am getting that error***
    var siteKey = Configuration.GetSection("GoogleRecaptcha").GetSection("RecaptchaV3SiteKey").Value ?? "";
}
<!DOCTYPE html>
<html lang="tr">
<head>
    <partial name="_HeadPartial" />
    <!-- RECAPTCHA JS-->
    <script src="https://www.google.com/recaptcha/api.js?render=@siteKey"></script>
    <!-- RECAPTCHA JS-->
</head>
It goes like that....

我已经检查了我的BaseController,它运行良好。

感谢您的任何建议!

【问题讨论】:

    标签: asp.net asp.net-mvc razor n-tier-architecture


    【解决方案1】:

    你可以做 return View(VieModel)

    然后在您的 .cshtml 视图中,您可以添加您传递给视图的类型的 @model

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-14
      • 1970-01-01
      • 1970-01-01
      • 2021-01-16
      • 2017-04-27
      相关资源
      最近更新 更多