[英]Html.Dropdownlistfor selected value is not working
我无法获取选定的值以与HTML.dropwdownlistfor一起使用
这是我要尝试开始的工作,所有数据都在那儿,select语句找到选定的项目,SelectListItems的ienumerable有一个具有“ select = true”的项目我不确定我要去哪里。
模型:
[SitecoreIgnore]
public virtual IEnumerable<Sitecore.Data.Items.Item> List_Location { get; set; }
public virtual Sitecore.Data.Items.Item Location { get; set; }
[SitecoreIgnore]
public virtual string Location_Model { get; set; }
帮助方法:
public static IEnumerable<SelectListItem> SelectListConstructor(Item selectedItem, IEnumerable<Item> options)
{
IEnumerable<SelectListItem> listings = from o in options
select new SelectListItem
{
Selected = (selectedItem.ID == o.ID),
Text = o.DisplayName,
Value = o.ID.ToString()
};
return listings;
}
控制器:(转换工作正常,数据在那里)
[HttpPost]
public ActionResult CreateAdStep(Classified model)
{
if(ModelState.IsValid)
{
return RedirectToAction("PreviewAd", model);
}
var allerrrors = ModelState.Values.SelectMany(v => v.Errors);
return View(Sitecore.Context.Database.GetItem("{69040A7B-5444-4ECF-A081-C4A3B26876B5}").GlassCast<Classified>());
}
[HttpGet]
public ActionResult CreateAdStep()
{
var trial = Sitecore.Context.Database.GetItem("{69040A7B-5444-4ECF-A081-C4A3B26876B5}").GlassCast<Classified>();
return View(trial);
}
CSHTML线路尝试:1)
@{ var emo = CalCPA.Source.Models.Utility.Utility.SelectListConstructor(Model.Location, Model.List_Location);
var emo2 = emo.Where(i=> i.Selected == true).FirstOrDefault();
}
@Html.DropDownListFor(m => m.Location_Model, new SelectList(emo, "Value", "Text", emo2.Value))
2)
@{ var emo = CalCPA.Source.Models.Utility.Utility.SelectListConstructor(Model.Location, Model.List_Location);
var emo2 = emo.Where(i=> i.Selected == true).FirstOrDefault();
}
@Html.DropDownListFor(m => m.Location_Model, new SelectList(emo, "Value", "Text", emo2))
3)
@{ var emo = CalCPA.Source.Models.Utility.Utility.SelectListConstructor(Model.Location, Model.List_Location);
var emo2 = emo.Where(i=> i.Selected == true).FirstOrDefault();
}
@Html.DropDownListFor(m => m.Location_Model, emo)
感谢您的任何见解/帮助!!!
如果要使用DropDownListFor
,则需要首先在模型中设置选定的值。
在您的操作(或模型构建器)中:
model.Location_Model = "my selected value";
然后你的看法:
@Html.DropDownListFor(m => m.Location_Model, new SelectList(emo, "Value", "Text"))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.