我正在制作一个多选列表,您可以在其中选择项目,然后单击"向上"或"向下"按钮,这将允许您对列表中的这些项目重新排序.
我有一个简单的自包含示例:
<html>
<head>
<title>Example</title>
<script src="https://www.google.com/jsapi"></script>
<script>
google.load('jquery', '1.4.1');
</script>
</head>
<body>
<select id="selectedDataPoints" multiple="multiple">
<option>Pig</option>
<option>Duck</option>
<option>Dog</option>
<option>Zebra</option>
<option>Snake</option>
<option>Giraffe</option>
<option>Cow</option>
</select>
<input type="button" id="btnReorderUp" value="Up" />
<input type="button" id="btnReorderDown" value="Down" />
</body>
</html>
<script type="text/javascript">
var DataPointSelector = (function() {
var $selectedList = $('#selectedDataPoints');
$('#btnReorderUp').click(function(e) {
moveUp();
e.preventDefault();
});
$('#btnReorderDown').click(function(e) {
moveDown();
e.preventDefault();
});
function moveUp() {
var select = $selectedList[0];
for (var i = 1, n = select.options.length; i < n; i++)
if (select.options[i].selected && !select.options[i …Run Code Online (Sandbox Code Playgroud) 我DropDownListFor用来在视图中呈现下拉列表.不知怎的,呈现的列表将不会选择SelectListItem与Selected设置为true.
在控制器动作中:
var selectList = sortedEntries.Select(entry => new SelectListItem
{
Selected = entry.Value.Equals(selectedValue),
Text = entry.Value,
Value = entry.Id
});
return View(new DropDownListModel
{
ListId = id,
SelectList = selectList,
OptionLabel = "Click to Select"
});
Run Code Online (Sandbox Code Playgroud)
在视图中:
<%= Html.DropDownListFor(m => m.ListId,
Model.SelectList,
Model.OptionLabel,
new {@class="someClass"}) %>
Run Code Online (Sandbox Code Playgroud)
我尝试过以下方法:
Selected设置为只有一个且只有一个项目true.SelectList在DropDownListFor: Html.DropDownListFor(m => m.ListId,
new SelectList(Model.SelectList, "Value", "Text",
new List<SelectListItem>(Model.SelectList).Find(s => s.Selected)),
new {@class="someClass"}) …Run Code Online (Sandbox Code Playgroud) asp.net-mvc selectlist selectlistitem html.dropdownlistfor drop-down-menu
我正在尝试理解c#ASP.NET MVC4并不断遇到SelectList.我似乎无法找到它的解释,除此之外:
http://msdn.microsoft.com/en-us/library/system.web.mvc.selectlist%28v=vs.108%29.aspx
任何人都可以给出一个简单的解释,并说明如何使用它?
正如你在这里和这里看到的,我不是asp.net MVC的SelectList的好朋友.
这次我想知道如何计算其中的项目.如果可能的项目不提供任何选择(items.count <2),我想显示标签而不是下拉列表.
- 编辑 -
虽然Will的回答可能也有效,但最简单的方法就是打电话
.GetListItems().Count()
Run Code Online (Sandbox Code Playgroud)
这会禁用你可能拥有的任何延迟抓取,但是因为他们无论如何都会进入下拉列表,我认为这不应该是一个问题.
我有一个ASP.NET MVC应用程序,有很多下拉列表和多选列表.基本上,很多选项列表.
我的问题是; 将这些列表作为模型的一部分或ViewData传递给视图会更好吗?
我目前正在将它们作为ViewData传递,因为我在模型上并不真正需要它们,并且它们似乎可能在模型上传递时很笨重(我得到所选项目或项目,这实际上是我所需要的).在缺点方面,ViewData需要在View上进行转换,这不如强类型模型好.
这里有最好的做法吗?即使是对这些方面的利弊建议也会受到赞赏.
我想从RSpec中的选择列表中选择值.例如,我有这样的数据:
<div class="control-group">
<label class="control-label" for="user_teacher_leader_attributes_teacher_id">Teacher names</label>
<div class="controls">
<select id="user_teacher_leader_attributes_teacher_id" name="user[teacher_leader_attributes][teacher_id]">
<option value="1" selected="selected">Math teacher</option>
<option value="2">Physics teacher</option>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
我想Physics teacher通过RSpec选择选项.我怎样才能做到这一点?此外,我可以按值从列表中选择一些内容(例如,选择Physics teacher它具有的值"2")吗?
有一个视图显示5个下拉列表,其中填充了相关表中的所有可用课程:
@model StudentRegistrationPortal.Models.CourseRegisterModel
@{
ViewBag.Title = "registerCourses";
}
<h2>Welcome
@Context.User.Identity.Name
</h2>
@Html.ActionLink("[Sign Out]", "SignOut", "Admin")
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<fieldset>
<legend>Following are available Courses - Please select Courses to Register</legend>
<table>
<tr>
<td>
<div class="editor-label">
Course-1:
</div>
</td>
<td>
<div class="editor-field">
@Html.DropDownListFor(m => m.Course.CId, Model.CoursesList)
</div>
</td>
</tr>
<tr>
<td>
<div class="editor-label">
Course-2:
</div>
</td>
<td>
<div class="editor-field">
@Html.DropDownListFor(m => m.Course.CId, Model.CoursesList)
</div>
</td>
</tr>
<tr>
<td>
<div class="editor-label">
Course-3:
</div>
</td>
<td>
<div class="editor-field">
@Html.DropDownListFor(m => m.Course.CId, Model.CoursesList)
</div> …Run Code Online (Sandbox Code Playgroud) 我需要建立一个SelectList从任何Enum在我的项目.
我有下面的代码,我从特定的枚举创建一个选择列表,但我想为任何枚举创建一个扩展方法.此示例检索DescriptionAttribute每个Enum值的值
var list = new SelectList(
Enum.GetValues(typeof(eChargeType))
.Cast<eChargeType>()
.Select(n => new
{
id = (int)n,
label = n.ToString()
}), "id", "label", charge.type_id);
Run Code Online (Sandbox Code Playgroud)
public static void ToSelectList(this Enum e)
{
// code here
}
Run Code Online (Sandbox Code Playgroud) 我的数据层中有一个枚举,我想在我的网站项目中使用它的下拉列表.我在数据层中的枚举是:
namespace SME.DAL.Entities.Enums
{
public enum EntityState
{
Open,
Freezed,
Canceled,
Completed,
Terminated,
ReadOnly
}
}
Run Code Online (Sandbox Code Playgroud)
如何制作其选择列表并在我的网站页面中使用它?我正在使用ASP.NET MVC 4.
我尝试过几种不同的方法.我不确定为什么但我的SelectList/DropDown是空的.它没有显示数据.我不确定我哪里出错了.
我有一个ASP.NET核心应用程序.实体框架核心.Db首先.我正在使用存储库模式.
这是我的模型类
public partial class Commodity
{
public Guid Oid { get; set; }
public string Code { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这是我的界面:
interface ICommodityRepository
{
IEnumerable<Commodity> GetAll();
}
Run Code Online (Sandbox Code Playgroud)
我的存储库:
public class CommodityRepository : ICommodityRepository
{
private ltgwarehouseContext context;
public CommodityRepository()
{ }
public IEnumerable<Commodity> GetAll()
{
return context.Commodity.ToList();
}
}
Run Code Online (Sandbox Code Playgroud)
我的控制器:
public class CommoditiesController : Controller
{
static readonly CommodityRepository commodities = new CommodityRepository();
public CommoditiesController(CommodityRepository commodities)
{ }
// GET: /<controller>/
public IEnumerable<Commodity> CommoditiesList()
{
return commodities.GetAll(); …Run Code Online (Sandbox Code Playgroud) selectlist ×10
asp.net-mvc ×7
c# ×4
enums ×2
asp.net ×1
asp.net-core ×1
html ×1
ienumerable ×1
javascript ×1
jquery ×1
model ×1
multi-select ×1
performance ×1
rspec ×1
viewdata ×1