你知道我可以轻松地将字符串数组的内容绑定到MVC Razor视图中的DropDownList吗?
public static string[] AgeRagne = new string[] { "Sun", "Mon", "Tues", "Wed" };
Run Code Online (Sandbox Code Playgroud)
更新:下面的代码工作.
@Html.DropDownListFor(
model => model.Filter.AgeRange,
new SelectList(Extensions.AgeRange, Model.Filter.AgeRange),
new { @class = "search-dropdown", name = "ageRange" }
)
Run Code Online (Sandbox Code Playgroud) 我正在努力更新应用程序以使用Kendo UI,并且遇到了使用DropDownList绑定到Enum的问题.我遇到的两个问题是:1)该值不包含Enum值,而是包含"Today"(应为0),以及2)显示值始终为"Last10Days"而不是"Last 10 Days"标签.我看了,找不到另一个地方,有人使用Kendo UI将描述显示为文本,并包含数值而不是文本.任何帮助表示赞赏.
<div class="span6">
@Html.LabelFor(m=> m.DateRanges)
@(Html.Kendo().DropDownListFor(m => m.DateRanges)
.BindTo(Enum.GetNames(typeof(SearchDateRanges)).ToList())
.HtmlAttributes(new { value = "Today" })
.DataTextField("Text")
.Events(e => e.Change("DateChange")))
</div>
<div class="span6">
@Html.LabelFor(m => m.Status)
@(Html.Kendo().DropDownListFor(m=> m.Status)
.BindTo(Enum.GetNames(typeof(SearchStatusCriteria)).ToList())
.HtmlAttributes(new {value = "All"}))
</div>
Run Code Online (Sandbox Code Playgroud)
public enum SearchDateRanges
{
[Description("Today")]
Today = 0,
[Description("Last 10 Days")]
Last10Days = 1,
/// <summary>
/// The last 30 days.
/// </summary>
[Description("Last 30 Days")]
Last30Days = 2,
[Description("Last 60 Days")]
Last60Days = 3,
[Description("Last 90 Days")]
Last90Days = 4, …
Run Code Online (Sandbox Code Playgroud) 我准备好解决这个问题.我是MVC的新手,但对它已经很熟悉了.
我试图在视图上放置一个下拉列表,但在调用DropDownListFor的行上不断获得"对象引用未设置为对象的实例".这就是我所拥有的:
模型
public class UserModel
{
public class DietObject
{
public int Id { get; set; }
public string Name { get; set; }
}
public IEnumerable<DietObject> DietOptions = new List<DietObject>
{
new DietObject { Id = 0, Name = "Meat" },
new DietObject { Id = 1, Name = "Vegetarian" }
};
[Required]
[StringLength(15)]
[Display(Name = "Diet:")]
public string Diet { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
调节器
[HttpGet]
public ActionResult Registration()
{
return View();
}
Run Code Online (Sandbox Code Playgroud)
视图
<div class="fHeader">
<%: …
Run Code Online (Sandbox Code Playgroud) asp.net-mvc html.dropdownlistfor asp.net-mvc-4 dropdownlistfor
在我的剃刀视图中,我使用下拉列表.我希望禁用此控件(不可选).
我的代码是:
<div class="field-list">@Html.DropDownListFor(model => model.LinguaCodiceMadre, Model.LinguaMadreList, new{ @disabled = "disabled" })</div>
Run Code Online (Sandbox Code Playgroud)
但它不起作用,我的控制始终启用.Html页面代码是:
<select name="LinguaCodiceMadre" id="LinguaCodiceMadre" data-val-length-max="10" data-val-length="The field LinguaCodiceMadre must be a string with a maximum length of 10." data-val="true">
<option></option>
<option value="sq">Albanian</option>
<option value="de">German</option>
<option value="en">English</option>
<option value="fr">French</option>
<option value="it">Italian</option>
<option value="pt">Portuguese</option>
<option value="ru">Russian</option>
<option value="es">Spanish</option>
</select>
Run Code Online (Sandbox Code Playgroud)
没有"禁用"属性.
我的真正目标是有条件地启用/禁用下拉列表,如下所示:
<div class="field-list">@Html.DropDownListFor(model => model.LinguaCodiceMadre, Model.LinguaMadreList, new{@disabled=Model.IsDisabled ? "disabled" : "false"})</div>
Run Code Online (Sandbox Code Playgroud)
但它不起作用.
我试过两个
new{@disabled=Model.IsDisabled ? "disabled" : "false"}
Run Code Online (Sandbox Code Playgroud)
和
new{disabled=Model.IsDisabled ? "disabled" : "false"}
Run Code Online (Sandbox Code Playgroud)
但没有,禁用属性不在html页面上呈现.
有人有想法吗?
我有这个填充ASP.NET MVC视图中的下拉列表.
<%= Html.DropDownListFor(model => model.Bikes,
Model.Bikes.Select(
x => new SelectListItem {
Text = x.Name,
Value = Url.Action("Details", "Bike", new { bikeId = x.ID }),
Selected = x.ID == Model.ID,
})) %>
Run Code Online (Sandbox Code Playgroud)
调试这个我可以看到该Selected
属性设置为true
应该的时间.但是,在呈现视图时,列表中的所有选项都未被选中.我意识到这可以通过另一个重载完成,DropDownListFor
但我真的想让这个版本工作.
有任何想法吗?
我按照这篇文章中的说明进行操作,但是当我尝试添加产品时,我收到此错误:
Server Error in '/' Application.
--------------------------------------------------------------------------------
Value cannot be null.
Parameter name: source
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ArgumentNullException: Value cannot be null.
Parameter name: source
Source Error:
Line 63: </div>
Line 64: <div class="editor-field">
Line 65: @Html.DropDownListFor(model => model.CategoryId, ((IEnumerable<GAM.Models.Category>)ViewBag.PossibleCategories).Select(option => new SelectListItem {
Line 66: Text = …
Run Code Online (Sandbox Code Playgroud) 我的stringlength验证总是在带有字符串值的下拉列表中失败.
这是我的模型:
[Required(ErrorMessage = "Required")]
[StringLength(2, MinimumLength = 2)]
[Display(Name = "Home Address State")]
public string HomeAddressState { get; set; }
Run Code Online (Sandbox Code Playgroud)
这是我的观点:
@Html.DropDownListFor(model => model.HomeAddressState, new SelectList(ViewBag.States, "Value", "Text"), string.Empty)
@Html.ValidationMessageFor(model => model.HomeAddressState)
Run Code Online (Sandbox Code Playgroud)
这是html输出:
<select data-val="true" data-val-length="The field Home Address State must be a string with a minimum length of 2 and a maximum length of 2." data-val-length-max="2" data-val-length-min="2" data-val-required="Required" id="HomeAddressState" name="HomeAddressState"><option value=""></option>
<option value="CA">California</option>
<option value="IL">Illinois</option>
<option value="IN">Indiana</option>
<option value="OH">Ohio</option>
</select>
Run Code Online (Sandbox Code Playgroud)
无论选择哪个选项,StringLength验证都会在客户端失败.我做错了什么?
我们可以从剃刀下拉列表控件中选择多个项目吗?即为
@ Html.DropDownListFor(m => m.Country,CountryList as SelectList," - Select--")
asp.net asp.net-mvc html.dropdownlistfor razor asp.net-mvc-3
我可以在插入时成功将值保存到数据库(标题值),但是当我在编辑模式下渲染同一视图时,标题字段必须保存所选值,但是在我的情况下,标题下拉列表没有选择任何值...不知道为什么我在标题字段保留存储的值(在后端)时没有选择任何内容的下拉菜单。
@Html.DropDownListFor(model => model.title, new SelectList(Model.titles, "Value", "Text"),"-Select-") // nothing selected on edit mode
@Model.title //displaying the stored value which the user selected initially.
Run Code Online (Sandbox Code Playgroud)
标题值
titles = new SelectList(ListItem.getValues().ToList(), "Value", "Text").ToList();
Run Code Online (Sandbox Code Playgroud)
getValue函数
public static List<TextValue> getValues()
{
List<TextValue> titles= new List<TextValue>();
TextValue T= new TextValue();
T.Value = "Mr";
T.Text = "Mr";
titles.Add(T);
T= new TextValue();
T.Value = "Mrs";
T.Text ="Mrs";
titles.Add(T);
T= new TextValue();
T.Value = "Miss";
T.Text = "Miss";
titles.Add(T);
T= new TextValue();
T.Value ="Other";
T.Text = "Other";
titles.Add(T); …
Run Code Online (Sandbox Code Playgroud) 我有一个名为 的模型属性的下拉列表SomProperty
:
@Html.DropDownListFor(model => model.SomeProperty,(SelectList)ViewBag.items)
Run Code Online (Sandbox Code Playgroud)
在控制器中,ViewBag.items
是一个IEnumerable<SelectListItem>
.
如何为下拉列表分配 ID?