在单个剃刀视图中,我如何检索从控制器的不同方法传递的值? [英] In a single razor view, how can i retrieve values passed from different methods of a controller?

查看:49
本文介绍了在单个剃刀视图中,我如何检索从控制器的不同方法传递的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm working on a basic application.

这是主控制器:

   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);
    }

在此控制器中,索引"和详细信息方法"分别连接到两个不同的剃刀视图,如下所示:

In this controller Index and Details Methods are connected to two different razor views as follows:

这是Index的剃刀视图

This is the razor view for Index

@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>

这是详细信息"的剃刀视图

This is the razor view for Details

@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>

这是mainViewModel

This is the mainViewModel

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

现在在上面的主控制器中,如果您注意到我有第三个方法作为count,则该方法连接到局部视图,如下所示:

Now in the main controller above, if you have noticed i have a third method as count which is connected to a partial view as follows:

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

countVm或(计数视图模型)如下:

And the countVm or (count view model) is as follows:

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

到目前为止,一切都很好, 现在,根据应用程序的要求,我必须将此count部分视图添加到所有其他剃刀视图中,如下所示:

Everything is working fine till this moment, Now as per application requirement i have to add this count partial view to all other razor views as follows:

 @Html.Partial("count")

但是当我将其添加到索引"或详细信息"剃刀视图中时,它会产生如下错误:

But when i add it into Index or Details razor views it generates an error as:

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'.

现在让我们说这个计数方法具有一些相同的数据,这些数据必须传递给所有其他剃刀视图,但不能分别传递,因为分开传递将很耗时,并且假设明天由于某种原因如果逻辑被更新,那么我必须分别访问每个方法并必须对其进行相应的更新,如果我们假设有100个以上的方法,则不可能.

Now lets say this count method has some identical data that has to be passed to all other razor views but not separately, because separately passing will be time consuming and suppose tomorrow due to any reason if the logic is updated then i have to go to each and every single method individually and have to update them accordingly, which is not possible if we are assuming more than 100 methods.

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

So in short, i am looking for a way to retrieve data from two view models in a single razor view?

推荐答案

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

Use ViewBag or ViewData to pass any kind of information when you are bounded to one Model.

  • ViewBag是动态的:@ViewBag. -只需在控制器中填写数据,您就可以在视图中看到它.

  • ViewBag is dynamic : @ViewBag. - just fill data in controller and you will have it in view.

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

ViewData - is a dictionary and use is ViewData["any_name"]

当然,视图中需要适当的投射.

Of course appropriate casting is required in a view.

如果要存储访问所有视图的全局变量,则该变量位于应用程序级别:

If you want to store global variable accessed to all views then it is on application level :

 Application["Counter"] = 1234;

您还可以将模型传递给partial:

Also you can pass model to partial :

@Html.Partial("count", Model)

这篇关于在单个剃刀视图中,我如何检索从控制器的不同方法传递的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆