简体   繁体   English

需要将Viewbag和TemplateInfo传递到MVC中的部分视图

[英]Need to pass Viewbag and TemplateInfo to partial view in MVC

So the question of how to pass Viewbag data to a partial view has been answered on here multiple times, but I need to pass the viewbag info and the TemplateInfo to my partial view and I can't find a single question that deals with that. 因此,如何将Viewbag数据传递到部分视图的问题已在这里得到了多次回答,但是我需要将Viewbag信息和TemplateInfo传递到我的部分视图,而我找不到解决该问题的单个问题。 Part of my controller looks like this. 我的控制器的一部分看起来像这样。

<div>
    @{Html.RenderPartial("AboutActivity", (Transport.Models.Tracking.TravelRequest.AboutActivityModel)Model,
              new ViewDataDictionary { TemplateInfo = new TemplateInfo { HtmlFieldPrefix = "AboutActivity" } });}
</div>

This works fine in mapping my objects back to the model and all, but I have a few views that need to load dropdowns from the ViewBag data as so. 这样可以很好地将对象映射回模型以及所有对象,但是我有一些视图需要从ViewBag数据中加载下拉列表。

@Html.DropDownList("Destination.State", (IEnumerable<SelectListItem>)ViewBag.States)

How can I pass both the ViewBag data and the TemplateInfo prefix to. 如何将ViewBag数据和TemplateInfo前缀传递给它。

You do not need to explicitly pass the ViewBag data from your main view to your partial view. 您无需将ViewBag数据从主视图显式传递到局部视图。 As long as you have it available in your main view( that means you set the ViewBag data in your GET action ), It will be available in the partial view as well. 只要您可以在主视图中使用它( 这意味着您在GET操作中设置了ViewBag数据 ),它在部分视图中也将可用。

public ActionResult Index()
{
   var states = new List<SelectListItem>
   {
        new SelectListItem {Value = "MI", Text = "MI"},
        new SelectListItem {Value = "OH", Text = "MOH"}           
   };
   ViewBag.States = states;
   return view();
}

And in your Index and the partial view which is being invoked from the Index view, ViewBag.States is accessible. 在索引和从索引视图中调用的局部视图中,可以访问ViewBag.States。

Index view 索引检视

@Html.DropDownList("Destination.State", (IEnumerable<SelectListItem>)ViewBag.States)
<div>
    @{ Html.RenderPartial("AboutActivity"); }
</div>

Partial view (AboutActivity ) 部分视图(AboutActivity

<h2>My PartialView</h2>
@Html.DropDownList("Destination.State", (IEnumerable<SelectListItem>)ViewBag.States)

A better solution is to not use dynamic stuff like ViewBag and ViewData to pass data between action methods and views. 更好的解决方案是不要使用诸如ViewBag和ViewData之类的动态内容在动作方法和视图之间传递数据。 Why not add a new property to your view model of type List<SelectListItem> for the dropdown data and use that ? 为什么不将新属性添加到下拉列表数据类型为List<SelectListItem>视图模型中并使用它呢?

public class AboutActivityViewModel
{
  public List<SelectListItem> States {set;get;}
  public int SelectedState {set;get;}    
  // Other properties for the view as needed
}

public ActionResult Index()
{
   var vm = new AboutActivityViewModel();
   vm.States = new List<SelectListItem>
   {
        new SelectListItem {Value = "MI", Text = "MI"},
        new SelectListItem {Value = "OH", Text = "MOH"}           
   };      
   return view(vm);
}

And your view which is strongly typed to AboutActivityViewModel 和您的视图,它被强类型为AboutActivityViewModel

@model AboutActivityViewModel
<div>
    @{ Html.RenderPartial("AboutActivity"); }
</div>

And in your Partial view, which is also strongly typed. 并且在您的Partial视图中,它也是强类型的。

@model AboutActivityViewModel     
@Html.DropDownListFor(f => f.SelectedState, Model.States)

If you do not pass a model to the partial view explicitly, It will use the model of the parent page from it is called. 如果没有将模型显式传递给部分视图,它将使用从其调用的父页面的模型。

In your case, you are passing a totally new ViewDataDictionary to the partial which will overwrite the States you set to the ViewBag earlier. 在您的情况下,您要将一个全新的ViewDataDictionary传递给该部分,它将覆盖您之前设置给ViewBag的状态。 So you should make sure that you are reading the States from existing ViewBag and set that to the new ViewDataDictionary you are passing. 因此,您应确保从现有的ViewBag中读取状态,并将其设置为要传递的新ViewDataDictionary

<div>

    @{ Html.RenderPartial("AboutActivity", Model, new ViewDataDictionary
        {
            TemplateInfo = new TemplateInfo { HtmlFieldPrefix = "AboutActivity" },
            ["States"] = ViewBag.States
        });
     }
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM