In a single razor view, how can i retrieve values

2019-02-26 05:54发布

问题:

I'm working on a basic application.

This is the main 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);
    }

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

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>

This is the mainViewModel

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

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>

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

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

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?

回答1:

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

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

  • 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;

Also you can pass model to partial :

@Html.Partial("count", Model)