从ViewBag传递一个值到一个局部视图 [英] Passing a Value from a ViewBag to a partial View

查看:1214
本文介绍了从ViewBag传递一个值到一个局部视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我的控制器的code是如下:

So my Controller's code is as follows:

private CommunityModelsContext dbCommunities = new CommunityModelsContext();

// GET: /Home/
public ActionResult Index()
{
     //retrieve the Communities 
     ViewBag.Communities = dbCommunities.Communities.ToList();
     return View();
}

和我查看了所有这些重要的行启动局部视图

And my View has this all important line to start the Partial View

<div id="LeftView" class="PartialView">@{Html.RenderPartial("CommunitiesPartial");}</div>

和在局部视图,我试图创建一个DropDownList(我还在学习,这是一种实践的应用程序只是为了看看我理解从asp.net教程中的概念),这将随后借此名单的实体,显示一个字段,从其他(名称和ID)

and in the Partial view, I'm trying to create a DropDownList (I'm still learning, this is a practice app just to see if I've understood the concepts from the asp.net tutorial), that will then take this List of entities, display one field, gets the value from the Other ("Name" and "id")

@model BuildingManagement.Models.Community.Community

@Html.BeginForm("Index","CommunityController")
{
    <div>
        @Html.LabelFor(x => x.Name)
        @Html.DropDownList("Community" , new SelectList(Model.Name,"id","Name"))
    </div>
}

现在,这将引发一个NullReference异常,型号为空。有在索引页没有模型,它不绑定到任何东西,但是,该数据正通过ViewBag发送

Now this throws a NullReference Exception, the model is null. There is no model in the Index page and it is not bound to anything, however, the data is being sent through the ViewBag.

想法,请?

推荐答案

您部分是强类型到模型( BuildingManagement.Models.Community.Community )。所以,你需要先通过这个模型的主要观点:

Your partial is strongly typed to a model (BuildingManagement.Models.Community.Community). So you need to pass this model to the main view first:

public ActionResult Index()
{
    //retrieve the Communities 
    ViewBag.Communities = dbCommunities.Communities.ToList();
    BuildingManagement.Models.Community.Community model = ... retrieve your model
    return View(model);
}

,然后因为你决定使用,而不是视图模型ViewBag您需要使用您在本ViewBag里面定义的值,让您的部分:

and then since you decided to use ViewBag instead of view models you need to keep using the value you defined in this ViewBag inside your partial:

@Html.DropDownList("Community", new SelectList(ViewBag.Communities, "id", "Name"))

当然是一个更好的方法是使用一个视图模型:

Of course a much better approach is to use a view model:

public class CommunityViewModel
{
    [DisplayName("Name")]
    public int Id { get; set; }
    public IEnumerable<SelectListItem> Communities { get; set; }
}

,然后让你的控制器填充视图模型,并通过该视图模型到视图:

and then have your controller populate the view model and pass this view model to the view:

public ActionResult Index()
{
    //retrieve the Communities 
    var communities = dbCommunities.Communities.ToList().Select(x => new SelectListItem
    {
        Value = x.Id.ToString(), 
        Text = x.Name
    })
    var model = new CommunityViewModel
    {
        Communities = communities
    }
    return View(model);
}

,然后让你的看法和偏强类型的视图模型:

and then make your view and partial strongly typed to the view model:

@model CommunityViewModel
@using (Html.BeginForm("Index","CommunityController"))
{
    <div>
        @Html.LabelFor(x => x.Id)
        @Html.DropDownListFor(x => x.Id, Model.Communities)
    </div>
}

这篇关于从ViewBag传递一个值到一个局部视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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