我使用了下拉列表中的模型,
> public class Items
> {
> public int itemcategoryid { get; set; }
> public string itemcategory { get; set; }
> public int Itemsubcategoryid { get; set; }
> public string Itemsubcategoryname { get; set; }
> }
并在控制器代码下方
> ItemViewModel catVM = new ItemViewModel();
> List<Items> catlist = catVM.GetCategoryInfo();
> ViewBag.categorylist = catlist;
使用HTML代码下方的绑定下拉列表后,该下拉列表已填充并选择默认名称作为itemcategory,但未传递itemcategory的ID,仅传递了POST的itemcategory名称。
@Html.DropDownListFor(model => model.itemcategory, new SelectList(ViewBag.categorylist, "itemcategory", "itemcategory", Model.itemcategory))
如果我在HTML代码下方使用其通过值,但未选择默认名称
@Html.DropDownListFor(model => model.itemcategory, new SelectList(ViewBag.categorylist, "itemcategoryid", "itemcategory", Model.itemcategory))
使用下面的代码检索数据
using (OracleCommand cmd = new OracleCommand("PS_Category", conn)) { cmd.CommandType = System.Data.CommandType.StoredProcedure; conn.Open(); OracleDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { Items listitems = new Items(); ; listitems.itemcategoryid = Convert.ToInt32(reader["CATID"]); listitems.itemcategory = reader["CATNAME"].ToString(); items.Add(listitems); } }
实际上我想选择默认名称并传递其ID怎么可能呢?
答案 0 :(得分:0)
之前在这里回答过,但有些人报告说这对他们不起作用; @Html.DropDownListFor how to set default value
如果这不起作用,请在此处选择列表类文档
这里提到您使用的构造函数使用最后一个参数作为选定值,因此您应该这样做; (最后传递model.itemcategoryid)
@Html.DropDownListFor(model => model.itemcategory, new SelectList(ViewBag.categorylist, "itemcategoryid", "itemcategory", Model.itemcategoryid))
如果上述答案对您不起作用,这是使用jquery的另一种方法;
selectLoop
; @Html.DropDownListFor(model => model.itemcategoryId, new SelectList(ViewBag.categorylist, "itemcategoryid", "itemcategory"), new { @class="selectLoop" })
@section scripts{
<script>
// when the document is ready (server is done loading)
$(document).ready(function(){
// loop through all those with selectLoop class
$(".selectLoop").each(function(){
// get the selected value and store to variable
var selected = "@Model.itemCategoryId";
// loop through all the option inside the select
$(this).find("option").each(function(){
// check if the value attribute matches the selected variable
if($(this).attr("value") == selected){
// if match, assign the option's selected attribute to true
$(this).attr("selected",true);
}
});
});
});
</script>
}
然后在您的shared / layout.cshtml中,在jquery和bootstrap之后添加@RenderSection("scripts", required: false)
。见下文;
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")
@RenderSection("scripts", required: false)
答案 1 :(得分:0)
对于多个下拉值,请使用它。对于插入值,请使用
ViewBag.DocumentId = new SelectList(_documentCategoryService.GetAll().OrderBy(a => a.Name), "Id", "Name");
对于编辑,您可以使用它。
ViewBag.DocumentId = new MultiSelectList(_documentCategoryService.GetAll(), "Id", "Name", _documentCategoryService.GetAllByExportId((int)id).Select(a => a.DocumentCategoryId).ToArray());
查看零件:
<div class="row-fluid">
<div class="span12">
<div class="control-group">
<label class="control-label">Documents Require</label>
<div class="controls controls-row">
@Html.DropDownList("DocumentId", null, htmlAttributes: new { @placeholder = "Select Documents", @class = "span6 chzn-select documentCategories", @tabindex = "8", @multiple = "multiple", @name = "DocumentId" })
</div>
</div>
</div>
</div>
对于单个DropdownValue。
ViewBag.BranchId = new SelectList(_branchService.GetAllByOrganizationId((User as CustomPrincipal).DefaultOrganizationId), "Id", "Name");
ViewBag.BranchId = new SelectList(_branchService.GetAllByOrganizationId((User as CustomPrincipal).DefaultOrganizationId), "Id", "Name", exportLetter.LcTransctionDetails.Transactions.BranchId);
<div class="span4">
<div class="control-group">
<label class="control-label">Branch</label>
<div class="controls controls-row">
@Html.DropDownList("BranchId", null, htmlAttributes: new { @class = "form-control", @id = "Branches" })
</div>
</div>
</div>