我发现了许多与此问题相关的问题和答案,但对我来说没有任何作用
我正在创建一个这样的下拉列表
@Html.DropDownListFor(m => m.SchoolYears, new SelectList(Model.SchoolYears, "YearId", "StartDates", Model.CurrentYearId), new { @class = "field_Dropdown", style = "width:100px;",onchange="return searchStudents();" })
Run Code Online (Sandbox Code Playgroud)
(Model.CurrentYearId属于类型int)
但它永远不会让当年选中.
从这篇文章中,我得到了我们应该使用字符串来选择值(虽然我不知道为什么因为它允许对象为选定值)
DropDownList SelectList SelectedValue问题
所以我尝试了所有这些变化
new SelectList(Model.SchoolYears, "YearId", "StartDates", Model.CurrentYearId.ToString())
new SelectList(Model.SchoolYears, "YearId", "StartDates", 2)
new SelectList(Model.SchoolYears, "YearId", "StartDates", "2")
Run Code Online (Sandbox Code Playgroud)
但他们甚至没有工作.
有办法通过linq查询或foreach循环创建选择列表并标记每个项目的选定属性,但为什么上述不起作用?
我正在返回一些存储的联系人来查看DropDownList,我无法在我的SelectList中包含多个dataTextFields.
目前我有:
@Html.DropDownListFor(model => model.Account.AccountContacts,
new SelectList(ViewBag.DDContacts, "Contact.ContactID", "Contact.FirstName"),
new { @class = "select", style = "width: 100px;" })
Run Code Online (Sandbox Code Playgroud)
我想拥有:
@Html.DropDownListFor(model => model.Account.AccountContacts,
new SelectList(ViewBag.DDContacts, "Contact.ContactID", "Contact.FullName"),
new { @class = "select", style = "width: 100px;" })
Run Code Online (Sandbox Code Playgroud)
它结合了FirstName和LastName属性.
更新:我知道扩展联系人属性,我很好奇是否有一种简化的方法来在视图或控制器中完成此操作.我在这里尝试了两种解决方案无济于事.如何在SelectList文本描述中组合两个字段?
asp.net-mvc selectlist html.dropdownlistfor razor asp.net-mvc-3
我正在研究ASP.NET MVC-4 Web应用程序.我在我的action方法中定义了以下内容来构建一个SelectList:
ViewBag.CustomerID = new SelectList(db.CustomerSyncs, "CustomerID", "Name");
Run Code Online (Sandbox Code Playgroud)
然后我在我的DropDownListFor内部渲染我View:
@Html.DropDownListFor(model => model.CustomerID, (SelectList)ViewBag.CustomerID, "please select")
Run Code Online (Sandbox Code Playgroud)
如图所示,我将ViewBag属性命名为等于Model属性名称CustomerID.从我自己的测试中,定义相同的名称不会导致任何问题或冲突,但我应该避免这种情况吗?
asp.net asp.net-mvc html-helper html.dropdownlistfor asp.net-mvc-4
我正在尝试使用选定的值制作下拉列表,但它不起作用:/我在网上搜索但我找不到解决方案:/
目前,我这样做:
在C#中:
ViewBag.ID_VEH = new SelectList(db.VEHI, "ID_VEH", "COD_VEH", 4); // 4 is an example
Run Code Online (Sandbox Code Playgroud)
在我的cshtml中:
@Html.DropDownListFor(model => model.ID_VEH, ViewBag.ID_VEH as SelectList)
Run Code Online (Sandbox Code Playgroud)
下拉列表已完成但未选择默认值:/您有什么想法吗?
在我的viewModel中我有:
public class PersonViewModel
{
public Person Person { get; set; }
public int SelectRegionId { get; set; }
public IEnumerable<SelectListItem> Regions { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
但是我在Controller/View中需要做些什么来显示值?我现在拥有的:
控制器:
public ActionResult Create()
{
var model = new ReUzze.Models.PersonViewModel
{
Person = new Person(),
Regions = new SelectList(this.UnitOfWork.RegionRepository.Get(), "Id", "Name")
};
return View(model);
}
Run Code Online (Sandbox Code Playgroud)
视图:
<div class="form-group">
@Html.LabelFor(model => model.Person.Address.Region)
@Html.DropDownListFor(model => model.SelectRegionId, new SelectList(Model.Regions, "Id", "Name"), "Choose... ")
</div>
Run Code Online (Sandbox Code Playgroud)
但它给出了这样的错误:
Cannot implicitly convert type 'System.Web.Mvc.SelectList' to 'System.Collections.Generic.IEnumerable<System.Web.WebPages.Html.SelectListItem>'. An explicit conversion …Run Code Online (Sandbox Code Playgroud) 我有一个非常简单的问题,有一个我无法找到的解决方案.
鉴于以下模型:
public class InfoViewModel
{
public SelectList States { get; set; }
[DisplayName("State")]
public string State { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我认为以下工作正常:
@Html.DropDownListFor(m => m.State, Model.States)
Run Code Online (Sandbox Code Playgroud)
但是,如果我尝试将其拉入编辑器模板(名为"SelectList"),例如:
@model System.String
@Html.DropDownListFor(m => m, ViewData["selectList"])
Run Code Online (Sandbox Code Playgroud)
然后使用EditorFor构建下拉列表:
@Html.EditorFor(m => m.State, "SelectList", new { selectList = Model.States })
Run Code Online (Sandbox Code Playgroud)
它失败并出现以下错误:
'System.Web.Mvc.HtmlHelper<string>' does not contain a definition for
'DropDownListFor' and the best extension method overload
'System.Web.Mvc.Html.SelectExtensions.DropDownListFor<TModel,TProperty>
(System.Web.Mvc.HtmlHelper<TModel>,
System.Linq.Expressions.Expression<System.Func<TModel,TProperty>>,
System.Collections.Generic.IEnumerable<System.Web.Mvc.SelectListItem>)'
has some invalid arguments
Run Code Online (Sandbox Code Playgroud)
我很难理解这两者之间的区别.我已尝试各种解决方法来排除故障,并获得相同的错误,或其他.
提前致谢.
asp.net-mvc editorfor html.dropdownlistfor razor asp.net-mvc-3
所以我有以下(伪代码):
string selectedvalud = "C";
List<SelectListItem> list= new List<SelectListItem>();
foreach(var item in mymodelinstance.Codes){
list.Add(new SelectListItem { Text = item.Name, Value = item.Id.Tostring(), Selected = item.Id.ToString() == selectedvalue ? true : false });
}
ViewBag.ListOfCodes = list;
Run Code Online (Sandbox Code Playgroud)
在我看来:
<%: Html.DropDownList("Codes", (List<SelectListItem>)ViewBag.ListOfCodes , new { style = "max-width: 600px;" })%>
Run Code Online (Sandbox Code Playgroud)
现在,在它到达视图之前,"列表"已经用项目填充它并标记了已经选择的项目.但是当它到达视图时,没有任何选项被标记为已选中.
我的问题是,是否可以使用viewbag来传递项目,还是应该使用不同的媒体?因为它删除选项上的选定标志,如果我这样使用它.
我有一个dropDownList在我看来,它是由填充clients表,表中包含样柱first_name,last_name,id等等,现在我想展示first_name并last_name作为显示文本和id在下拉列表中的值,我有做id的价值和first_name作为显示文本,但在这里我想组合这些列(first_name和last_name)并用作显示文本.
在模型中
function getClients()
{
$Clients = Client::model()->findAll();
$list = CHtml::listData($Clients , 'client_id', 'first_name');
return $list;
}
Run Code Online (Sandbox Code Playgroud)
在视野中
echo $form->dropDownList($model,'client_id',$model->getClients());
Run Code Online (Sandbox Code Playgroud) 我想知道,在MVC 4中创建下拉列表的最佳方法是什么?使用ViewBag或其他方法?
viewdata html-select html.dropdownlistfor viewbag asp.net-mvc-4
我使用我的枚举渲染一个下拉列表框,我只有3个选项,但由于某种原因它显示四个.top和default选项只是空白/空,我希望删除它.我希望top/default值为'Option1'.
枚举:
public enum EventType
{
[Display(Name = "Option 1")]
Option1,
[Display(Name = "Option 2")]
Option2,
[Display(Name = "Option 3")]
Option3
}
Run Code Online (Sandbox Code Playgroud)
视图:
@Html.EnumDropDownListFor(model => model.EventType, null, new { @id = "eventType", @class = "form-control" })
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助.
asp.net-mvc ×7
razor ×4
viewbag ×3
asp.net ×2
c# ×2
html-helper ×2
html-select ×2
selectlist ×2
editorfor ×1
php ×1
viewdata ×1
yii ×1
yii-cmodel ×1