WebGrid() 中 MVC 3 DropDownList() 的问题
我正在尝试将 DropDownList
放入 WebGrid
中,但我不知道如何做到这一点:(
我尝试过的事情:
grid.Column("Id", "Value", format: ((item) =>
Html.DropDownListFor(item.SelectedValue, item.Colors)))
以及
grid.Column(header: "", format: (item => Html.DropDownList("Colors", item.Colors)))
和
grid.Column(header: "", format: Html.DropDownList("Colors"))
以及其他各种但是我无法让它发挥作用。 非常感谢任何帮助。
模型
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; }
}
控制器
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);
}
视图
@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>
}
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>
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
另外,在控制器中,请确保设置 SelectList 的值和文本属性,而不是:
您应该使用:
另外,为所有行项目定义相同的
SelectList
对我来说似乎有点浪费,特别是如果它们包含相同的值。我会稍微重构您的视图模型:然后:
在视图中:
更新:
因为我怀疑您没有将这些下拉列表用于在视图中进行绘画和娱乐,但您希望用户在其中选择值,并且当他提交您可能希望获取所选值的表单,您将需要生成这些下拉列表的正确名称,以便默认模型绑定器可以自动检索 POST 操作中的所选值。不幸的是,由于 WebGrid 帮助程序有点糟糕并且不允许您检索当前行索引,因此您 可以使用 hack,正如 Haacked 在他的博客文章中所示:
现在您可以有一个 POST 控制器操作,在该操作中,当表单被打开时,您将获取每行的选定值提交:
Also in your controller make sure you set the value and text properties of your SelectList and instead of:
you should use:
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:and then:
and in the view:
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:
Now you could have a POST controller action in which you will fetch the selected value for each row when the form is submitted:
..使我的下拉列表在加载时从列表中选择一个值。以下内容肯定有效。我自己也尝试过。有任何问题请随时联系我。
..make my dropdown list to select a value from the list when it loads.The following works for sure. I have tried it out myself. Any questions keep me posted.