c# - 在单个 Razor View 中,如何检索从 Controller 的不同方法传递的值?

标签 c# asp.net-mvc razor partial-views

我正在开发一个基本应用程序。

这是主 Controller :

   public ActionResult Index()
    {
        var all = _context.mainz.ToList();
        var vm = new mainViewModel()
        {
            main_lst = all
        };

        return View(vm);
    }

    public ActionResult Details()
    {
        var dtl = _context.mainz.ToList();
        var vm = new mainViewModel()
        {
            main_lst = dtl
        };
        return View(vm);
    }


       public ActionResult count()
    {
        var ct = (from i in _context.mainz
                  where i.event_title != null
                  select i).ToList();
        var vm = new countVm()
        {
          count = ct
        };
        return View(vm);
    }

在此 Controller 中,索引和详细信息方法连接到两个不同的 Razor View ,如下所示:

这是索引的 Razor View

@model testtt.ViewModel.mainViewModel

@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}



<ul>
@foreach (var item in Model.main_lst)
{
    <li>@item.event_title</li>
}

</ul>

这是详细信息的 Razor View

@model testtt.ViewModel.mainViewModel
@{
ViewBag.Title = "Details";
Layout = "~/Views/Shared/_Layout.cshtml";
 }

<ul>
@foreach (var item in Model.main_lst)
{
   <li>@item.event_description</li>
}
</ul>

这是主ViewModel

namespace testtt.ViewModel
{
public class mainViewModel
{
    public List<main> main_lst { get; set; }
    public mainViewModel()
    {
        main_lst = new List<main>();
    }
}
}

现在在上面的主 Controller 中,如果您注意到我有第三种方法作为 count,它连接到部分 View ,如下所示:

@model testtt.ViewModel.countVm
<p>count is @Model.count.Count()</p>

countVm或(计数 View 模型)如下:

namespace testtt.ViewModel
{
public class countVm
{
    public List<main> count { get; set; }
    public countVm()
    {
        count = new List<main>();
    }
}
}

到目前为止一切正常, 现在,根据应用程序要求,我必须将此计数部分 View 添加到所有其他 Razor View ,如下所示:

 @Html.Partial("count")

但是当我将其添加到索引或详细信息 Razor View 中时,它会生成错误:

The model item passed into the dictionary is of type 'testtt.ViewModel.mainViewModel', but this dictionary requires a model item of type 'testtt.ViewModel.countVm'.

现在假设这个计数方法有一些相同的数据,必须传递给所有其他 Razor View ,但不能单独传递,因为单独传递将非常耗时,并且假设明天由于任何原因更新逻辑,那么我必须单独访问每个方法并必须相应地更新它们,如果我们假设超过 100 个方法,这是不可能的。

简而言之,我正在寻找一种在单个 Razor View 中从两个 View 模型检索数据的方法?

最佳答案

当您绑定(bind)到一个模型时,使用 ViewBag 或 ViewData 传递任何类型的信息。

  • ViewBag 是动态的:@ViewBag。 - 只需在 Controller 中填写数据,您就会看到它。

  • ViewData - 是一个字典,使用的是 ViewData["any_name"]

当然, View 中需要进行适当的转换。

如果您想存储可访问所有 View 的全局变量,那么它位于应用程序级别:

 Application["Counter"] = 1234;

您还可以将模型传递给部分:

@Html.Partial("count", Model)

关于c# - 在单个 Razor View 中,如何检索从 Controller 的不同方法传递的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53802276/

相关文章:

c# - 绘制树的算法,其中节点可以附加到深度大于自身 + 1 的其他节点

c# - 在 Windows Azure 中为用户帐户数据库设置分区和行键

c# - 如果您离开 Controller ,请清除 session

javascript - 困惑的 Javascript 帖子

c# - javascript 未在 ASP.NET MVC 中调用

c# - 删除列表中最后一次出现的字符串

c# - TypeLoadException : Method 'Create' in type . ..没有实现(错误如下)

c# - 混合 knockout 和 Razor 变量

c# - 是否可以对查询参数使用 Web API 模型验证?

c# - 输入无效的 MVC5 占位符