我想知道是否有人可以解决这个问题..
我有一个选项组下拉列表用于选择一个人的种族 - 但是它没有将值存储在模型中.
视图模型
[UIHint("EthnicOriginEditorTemplate")]
[DisplayName("Question 6: Ethnic Origin")]
public int EthnicOrigin { get; set; }
Run Code Online (Sandbox Code Playgroud)
助手:GroupDropList.Cs
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.Routing;
namespace Public.Helpers
{
public static class GroupDropListExtensions
{
public static string GroupDropList(this HtmlHelper helper, string name, IEnumerable<GroupDropListItem> data, int SelectedValue, object htmlAttributes)
{
if (data == null && helper.ViewData != null)
data = helper.ViewData.Eval(name) as IEnumerable<GroupDropListItem>;
if (data == null) return string.Empty;
var select = new TagBuilder("select");
if (htmlAttributes != null)
select.MergeAttributes(new …Run Code Online (Sandbox Code Playgroud) 如何让我的DropDownListFor支持optgroup?无论如何要做到这一点?请注意,这是DropDownListFor,表示它支持DataAnnotation客户端验证
我有一个简单的下拉列表,列表中的第一项有一个空值.如果我没有在列表中选择任何内容,则客户端验证会忽略它.我使用注释属性在模型上根据需要设置了该字段.
@Html.DropDownListFor(model => Model.CCPayment.State, UnitedStatesStates.StateSelectList)
[Required(ErrorMessage = "State is Required.")]
public string State
{
get
{
return _state;
}
set
{
_state = value;
}
}
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?我错过了什么吗?
validation jquery unobtrusive-validation asp.net-mvc-3 drop-down-menu
我没有与Helpers合作的经验,所以我有点不习惯使用手头的代码.
我的要求很简单,我只需要DropDownListFor扩展方法中的optgroup功能.在搜索时,我遇到了这个答案并将其复制到名为MyExtensionClass.cs的文件中.
但是,我不知道如何使用它或调用此中定义的扩展方法.请告诉我如何在列表中使用它.
现在,以下是我想要使用扩展方法的选择列表的控制器代码.
ViewBag.ParentCategoryId = new SelectList(db.Categories, "Id", "Name");
Run Code Online (Sandbox Code Playgroud)
这是我的观看代码
@Html.DropDownListFor(model => model.Product.CategoryId,
(IEnumerable<SelectListItem>)ViewBag.CategoryId, "---Choose Category---",
new { @class = "required" })
Run Code Online (Sandbox Code Playgroud)
请帮我升级到optgroup的扩展方法.
我有这个HTML ...
<select id="View" name="View">
<option value="1">With issue covers</option>
<option value="0">No issue covers</option>
</select>
Run Code Online (Sandbox Code Playgroud)
它不会让我插入这样的代码......
<select id="View" name="View">
<option value="1" <% ..logic code..%> >With issue covers</option>
<option value="0" <% ..logic code..%> >No issue covers</option>
</select>
Run Code Online (Sandbox Code Playgroud)
那么设置一个被选中的最佳方式是什么?
更新:不使用HTML帮助程序.