在每个SelectList项目上设置Selected = True不会影响呈现的ListBox [英] Setting Selected=True on each SelectList item does not affect rendered ListBox
问题描述
我整天都在忙这个,我正处于最后的障碍.基本上,我在其中一种表单上有一个多选列表,我在其中填充了数据库中的数据.我在模型中定义了一个小部件"属性作为选择列表.
I've been messing around with this one all day and I'm at the last hurdle. Basically I've got a multiple select list on one of my forms which I'm populating with data from my database. I've got a 'Widgets' property defined in my model as a selectlist.
然后在将数据库中的属性绑定到模型的部分中,我得到了linq表达式,该表达式将另一个表中的属性添加到我的选择列表中.一切正常,我可以使用结果正确发回.但是,如果我在linq语句中设置了selected = true属性,则另一端没有任何选择.所以我的问题是为什么?其他所有属性,例如id等都可以通过.
Then in the section where I bind properties from my database to my model, I've got linq expression which adds properties from another table to my selectlist. This is all working fine and I can use the result to correctly post back. However if I set the selected = true property in my linq statement nothing gets selected on the other end. So my question is why? All the other properties, id's etc.. are getting passed through fine.
我的模特:
[DisplayName("Article Id")]
public int ArticleId { get; set; }
[DisplayName("Article Title")]
public string ArticleTitle { get; set; }
[DisplayName("Article Body")]
public string ArticleBody { get; set; }
[DisplayName("Publish on Site")]
public bool Published { get; set; }
[DisplayName("Add to Featured Articles")]
public bool Featured { get; set; }
[DisplayName("Created Date")]
public DateTime CreatedDate { get; set; }
public int AuthorId { get; set; }
[DisplayName("Author")]
public string Author { get; set; }
[DisplayName("Modified Date")]
public Nullable<DateTime> ModifiedDate { get; set; }
public int OwnerId { get; set; }
public SelectList Widgets { get; set; }
我的控制器(Relevent位)
My Controller (Relevent bit)
ArticlesModel Obj = new ArticlesModel
{
ArticleId = row.ArticleId,
ArticleTitle = row.ArticleTitle,
ArticleBody = row.ArticleBody,
Featured = row.Featured,
Published = row.Published,
Widgets = new SelectList(db.Query("SELECT * FROM [Widgets]").Select(x => new SelectListItem { Text = x.WidgetName, Value = ((int)x.WidgetId).ToString(), Selected = true }), "Value", "Text")
};
Obj.SetGlobalProperties(this.ControllerContext.HttpContext.Application);
ViewData["Article"] = Obj;
最后是视图
@Html.ListBoxFor(m => m.Widgets, Model.Widgets, new { @class = "form-control", @style = "margin-left: -20px !important; width: 500px" })
真的很感谢这里的任何帮助-我整天都在拉头发,看上去很简单.
Would really appreciate any help here - I've been pulling my hair out all day with something that on the face of it seems quite simple.
预先感谢
推荐答案
我认为您不应将可用选项
与所选选项
混合使用.
I think you should not mix the available options
with the selected options
.
您需要添加另一个属性来保存选定的选项
,并将可用选项
的类型从 SelectList
更改为 SelectListItem []
.
You need to add another property for saving the selected options
and change the available options
type from SelectList
to SelectListItem[]
.
public SelectListItem[] Widgets { get; set; }
public int[] SelectedWidgets { get; set; } // depends on WidgetId type
将其更改为 SelectListItem []
.
Widgets = db.Query("SELECT * FROM [Widgets]")
.Select(x => new SelectListItem
{
Text = x.WidgetName,
Value = ((int)x.WidgetId).ToString(),
Selected = true
})
.ToArray()
html
@Html.ListBoxFor(m => m.SelectedWidgets, Model.Widgets)
这篇关于在每个SelectList项目上设置Selected = True不会影响呈现的ListBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!