使用ASP.NET MVC ViewBag和DropDownListfor有困难 [英] Having difficulty using an ASP.NET MVC ViewBag and DropDownListfor

查看:120
本文介绍了使用ASP.NET MVC ViewBag和DropDownListfor有困难的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的困难是如何使用 ViewBag DropdownListFor

My difficulty is how to use ViewBag with DropdownListFor?

在我的控制器我有:

TestModel model = new TestModel();
ViewBag.Clients = model.Clients;
ViewBag.StatusList = model.StatusList;
ViewBag.enumStatus = model.enumStatus;
ViewBag.intClient = model.intClient;

在我TestModel

In my TestModel

public SelectList Clients { get; set; }       
public SelectList StatusList { get; set; }
public ActiveStatus enumStatus { get; set; }
public int? intClient { get; set; }

在我的视图

我想用 DropDownListFor 显示ViewBag值,我该怎么办呢?

I want to use DropDownListFor to display the ViewBag values, how can I do that?

推荐答案

个人......我创建了一个列表,并做到这一点。

Personally...I create a List and do this.

public ActionResult SomeAction()
{
    var list = new List<SelectListItem>();
    list.Add(new SelectListItem(){Text = "One", Value="One"});
    list.Add(new SelectListItem(){Text = "Two", Value="Two"});
    list.Add(new SelectListItem(){Text = "Three", Value="Three"});
    list.Add(new SelectListItem(){Text = "Four", Value="Four"});

    ViewBag.Clients = list;

    return View();
}

,然后在您的视图...

and then in your view...

@Html.DropDownListFor(x => x.SomePropertyOnModel, (IEnumerable<SelectListItem>)ViewBag.Clients);

注意在Viewbag项目投。演员是必需的,因为viewbag不知道对象是什么 Viewbag.Client 。所以剧组有要求。

这篇关于使用ASP.NET MVC ViewBag和DropDownListfor有困难的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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