与MVC 3 DropDownList的()中的WebGrid)问题( [英] Problems with MVC 3 DropDownList() in WebGrid()

查看:99
本文介绍了与MVC 3 DropDownList的()中的WebGrid)问题(的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将的DropDownList 的WebGrid ,但我想不出该怎么办它:(

事情我已经试过:

  grid.Column(ID,值,格式为:((项目)=>
   Html.DropDownListFor(item.SelectedValue,item.Colors)))

  grid.Column(标题:,格式为:(项目=> Html.DropDownList(颜色,item.Colors)))

  grid.Column(标题:,格式:Html.DropDownList(颜色))

和各种各样其他,但我无法得到它的工作。
任何帮助深表AP preciated。

模型

 公共类PersonViewModel
{
    公共字符串名称{;组; }
    公众诠释年龄{搞定;组; }
    公众的SelectList颜色{搞定;组; }
    公众诠释的SelectedValue {搞定;组; }
}
公共类ColorViewModel
{
    公众诠释ColorID {搞定;组; }
    公共字符串ColorName {搞定;组; }
}

控制器

 公众的ActionResult指数()
{    VAR colorList =新的List< ColorViewModel>(){
                        新ColorViewModel(){ColorID = 1,ColorName =绿色},
                        新ColorViewModel(){ColorID = 2,ColorName =红色},
                        新ColorViewModel(){ColorID = 3,ColorName =黄色}
                    };    变种人=新的List< PersonViewModel>()
                {
                    新PersonViewModel(){
                        NAME =富,
                        年龄= 42,
                        颜色=新的SelectList(colorList)
                    },
                    新PersonViewModel(){
                        NAME =酒吧,
                        年龄= 1337,
                        颜色=新的SelectList(colorList)
                    }
                };    返回查看(人);
}

查看

  @model IEnumerable的< PersonViewModel>@ {
    VAR电网=新的WebGrid(模型);
}< H2>的DropDownList中的WebGrid< / H>
@using(Html.BeginForm())
{
    @ grid.GetHtml(
        列:grid.Columns(
            grid.Column(姓名),
            grid.Column(时代),
            grid.Column()// HELP - INSERT DROPDOWNLIST
        )
    )
    &所述p为H.;
        <按钮和GT;提交< /按钮>
    &所述; / P>
}


解决方案

  grid.Column(
    落下,
    格式:@<跨度> @ Html.DropDownList(颜色,(的SelectList)item.Colors)LT; / SPAN>

另外,在你的控制器确保你设定的SelectList的价值和文本属性,取而代之的:

 颜色=新的SelectList(colorList)

你应该使用:

 颜色=新的SelectList(colorList,ColorID,ColorName)

此外,它似乎有点浪费我的定义相同的SelectList 所有行项目尤其是如果它们包含相同的值。我会重构你的视图模型了一下:

 公共类MyViewModel
{
    公共IEnumerable的< SelectListItem>颜色{搞定;组; }
    公共IEnumerable的< PersonViewModel>人们{搞定;组; }
}公共类PersonViewModel
{
    公共字符串名称{;组; }
    公众诠释年龄{搞定;组; }
    公众诠释的SelectedValue {搞定;组; }
}

和则:

 公共类HomeController的:控制器
{
    公众的ActionResult指数()
    {
        VAR模型=新MyViewModel
        {
            //定义用于呈现下拉列表中的值
            //对于每一行
            颜色=新[]
            {
                新SelectListItem {值=1,文本=绿色},
                新SelectListItem {值=2,文本=红色},
                新SelectListItem {值=3,文本=黄色},
            },            //这是我们将在绑定的WebGrid的收藏
            人=新[]
            {
                新PersonViewModel {名称=富,年龄= 42},
                新PersonViewModel {名称=酒吧,年龄= 1337},
            }
        };        返回查看(模型);
    }
}

和视图:

  @model MyViewModel@ {
    VAR电网=新的WebGrid(Model.People);
}< H2>的DropDownList中的WebGrid< / H>@using(Html.BeginForm())
{
    @ grid.GetHtml(
        列:grid.Columns(
            grid.Column(姓名),
            grid.Column(时代),
            grid.Column(
                落下,
                格式:@<跨度> @ Html.DropDownList(颜色,Model.Colors)LT; / SPAN>
            )
        )
    )
    &所述p为H.;
        <按钮和GT;提交< /按钮>
    &所述; / P>
}


更新:

由于我怀疑你是不是把这些dropdownlists对你的看法绘画和乐趣,但你希望用户选择在他们里面的值,当他提交表单,你可能希望获取选定的值,则需要这些产生下拉列表中适当的名称,以便默认模型绑定会自动在你的POST操作检索所选值。不幸的是,因为辅助性的WebGrid还挺吸不允许您检索当前行的索引,你的可以使用黑客作为Haacked显示,他的博客:

  grid.Column(
    落下,
    格式:
        @<跨度>
            @ {VAR指数= Guid.NewGuid()的ToString(); }
            @ Html.Hidden(People.Index指数)
            @ Html.DropDownList(人[+指数+] .SelectedValueModel.Colors)
        < / SPAN>

现在你可以有一个POST控制器动作中,你会为当表单提交每一行获取所选值:

  [HttpPost]
公众的ActionResult指数(MyViewModel模型)
{
    //模型变量将包含人收藏
    //自动绑定包含所选值的每一行
    ...
}

I'm trying to place a DropDownList inside a WebGrid but I can't figure out how to do it :(

Things I've tried:

grid.Column("Id", "Value", format: ((item) =>
   Html.DropDownListFor(item.SelectedValue, item.Colors)))

and

grid.Column(header: "", format: (item => Html.DropDownList("Colors", item.Colors)))

and

grid.Column(header: "", format: Html.DropDownList("Colors"))

and various others but I couldn't get it to work. Any help is much appreciated.

Model

public class PersonViewModel
{
    public string Name { get; set; }
    public int Age { get; set; }
    public SelectList Colors { get; set; }
    public int SelectedValue { get; set; }
}
public class ColorViewModel
{
    public int ColorID { get; set; }
    public string ColorName { get; set; }
}

Controller

public ActionResult Index()
{

    var colorList = new List<ColorViewModel>() {
                        new ColorViewModel() { ColorID = 1, ColorName = "Green" },
                        new ColorViewModel() { ColorID = 2, ColorName = "Red" },
                        new ColorViewModel() { ColorID = 3, ColorName = "Yellow" }
                    };

    var people = new List<PersonViewModel>()
                {
                    new PersonViewModel() {
                        Name = "Foo", 
                        Age = 42, 
                        Colors = new SelectList(colorList)
                    },
                    new PersonViewModel() {
                        Name = "Bar", 
                        Age = 1337, 
                        Colors = new SelectList(colorList)
                    }
                };

    return View(people);
}

View

@model IEnumerable<PersonViewModel>

@{
    var grid = new WebGrid(Model);
}

<h2>DropDownList in WebGrid</h2>
@using (Html.BeginForm())
{
    @grid.GetHtml(
        columns: grid.Columns(
            grid.Column("Name"),
            grid.Column("Age"),
            grid.Column() // HELP - INSERT DROPDOWNLIST
        )
    )    
    <p>
        <button>Submit</button>
    </p>
}

解决方案

grid.Column(
    "dropdown", 
    format: @<span>@Html.DropDownList("Color", (SelectList)item.Colors)</span>
)

Also in your controller make sure you set the value and text properties of your SelectList and instead of:

Colors = new SelectList(colorList)

you should use:

Colors = new SelectList(colorList, "ColorID", "ColorName")

Also it seems a bit wasteful to me to define the same SelectList for all row items especially if they contain the same values. I would refactor your view models a bit:

public class MyViewModel
{
    public IEnumerable<SelectListItem> Colors { get; set; }
    public IEnumerable<PersonViewModel> People { get; set; }
}

public class PersonViewModel
{
    public string Name { get; set; }
    public int Age { get; set; }
    public int SelectedValue { get; set; }
}

and then:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var model = new MyViewModel
        {
            // define the values used to render the dropdown lists
            // for each row
            Colors = new[]
            {
                new SelectListItem { Value = "1", Text = "Green" },
                new SelectListItem { Value = "2", Text = "Red" },
                new SelectListItem { Value = "3", Text = "Yellow" },
            },

            // this is the collection we will be binding the WebGrid to
            People = new[]
            {
                new PersonViewModel { Name = "Foo", Age = 42 },
                new PersonViewModel { Name = "Bar", Age = 1337 },
            }
        };

        return View(model);
    }
}

and in the view:

@model MyViewModel

@{
    var grid = new WebGrid(Model.People);
}

<h2>DropDownList in WebGrid</h2>

@using (Html.BeginForm())
{
    @grid.GetHtml(
        columns: grid.Columns(
            grid.Column("Name"),
            grid.Column("Age"),
            grid.Column(
                "dropdown", 
                format: @<span>@Html.DropDownList("Color", Model.Colors)</span>
            )
        )
    )    
    <p>
        <button>Submit</button>
    </p>
}


UPDATE:

And since I suspect that you are not putting those dropdownlists for painting and fun in your views but you expect the user to select values inside them and when he submits the form you might wish to fetch the selected values, you will need to generate proper names of those dropdown lists so that the default model binder can automatically retrieve the selected values in your POST action. Unfortunately since the WebGrid helper kinda sucks and doesn't allow you to retrieve the current row index, you could use a hack as the Haacked showed in his blog post:

grid.Column(
    "dropdown", 
    format: 
        @<span>
            @{ var index = Guid.NewGuid().ToString(); }
            @Html.Hidden("People.Index", index)
            @Html.DropDownList("People[" + index + "].SelectedValue", Model.Colors)
        </span>
)

Now you could have a POST controller action in which you will fetch the selected value for each row when the form is submitted:

[HttpPost]
public ActionResult Index(MyViewModel model)
{
    // the model variable will contain the People collection
    // automatically bound for each row containing the selected value
    ...
}

这篇关于与MVC 3 DropDownList的()中的WebGrid)问题(的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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