在这篇文章中,我们将带领您了解@Html.DropDownList用于设置默认值的方法的全貌,包括怎么设置dropdownlist默认值的相关情况。同时,我们还将为您介绍有关@Html.DropDow
在这篇文章中,我们将带领您了解@ Html.DropDownList用于设置默认值的方法的全貌,包括怎么设置dropdownlist默认值的相关情况。同时,我们还将为您介绍有关@Html.DropDownListFor 下拉框绑定(选择默认值)、Ajax联动动态为@Html.DropDownListFor赋值、ASP.NET MVC 让@Html.DropDownList显示默认值、ASP.NET MVC4.0 DropDownListFor 设置默认选项的知识,以帮助您更好地理解这个主题。
本文目录一览:- @ Html.DropDownList用于设置默认值的方法(怎么设置dropdownlist默认值)
- @Html.DropDownListFor 下拉框绑定(选择默认值)
- Ajax联动动态为@Html.DropDownListFor赋值
- ASP.NET MVC 让@Html.DropDownList显示默认值
- ASP.NET MVC4.0 DropDownListFor 设置默认选项
@ Html.DropDownList用于设置默认值的方法(怎么设置dropdownlist默认值)
这个问题已经在这里有了答案 :
MVC5-如何在DropDownListFor Html helper中设置“ selectedValue”
(3个答案)
2年前关闭。
@Html.DropDownListFor(model => model.Status, new List<SelectListItem> { new SelectListItem{Text="Active", Value="True"}, new SelectListItem{Text="Deactive", Value="False"}})
鉴于我正在使用这种下拉dowenlist编码。我运行我的应用程序,默认非活动值显示在下拉列表框中。我想显示默认的 活动
答案1
小编典典像这样:
@Html.DropDownListFor(model => model.Status, new List<SelectListItem> { new SelectListItem{Text="Active", Value="True"}, new SelectListItem{Text="Deactive", Value="False"}},"Select One")
如果要默认选择“活动”,则使用以下Selected
属性SelectListItem
:
@Html.DropDownListFor(model => model.Status, new List<SelectListItem> { new SelectListItem{Text="Active", Value="True",Selected=true}, new SelectListItem{Text="Deactive", Value="False"}},"Select One")
如果使用SelectList
,则必须使用此重载并指定SelectListItem
Value
要设置为selected的属性:
@Html.DropDownListFor(model => model.title, new SelectList(new List<SelectListItem> { new SelectListItem { Text = "Active" , Value = "True"}, new SelectListItem { Text = "InActive", Value = "False" } }, "Value", // property to be set as Value of dropdown item "Text", // property to be used as text of dropdown item "True"), // value that should be set selected of dropdown new { @})
@Html.DropDownListFor 下拉框绑定(选择默认值)
首先先构建绑定下拉框的数据源
private void GetSalesList()
{
var userList = _rmaExpressAppService.GetUserList();
TempData["RMASalesList"] = new SelectList(userList, "Id", "UserName");//选择 userList中的Id作为 Value,选择·UserName 作为Text显示
}
然后构建前台,推荐用Html.DropDownListFor , model.Model.SaleExecutor 既可以在选择的时候将对应的Value传回去,也可以在初始化的时候给字段赋默认值
@Html.DropDownListFor(model => model.Model.SaleExecutor, TempData["RMASalesList"] as SelectList, new { @class = "form-control" })
注意初始化的时候要先赋值给前台
/// <summary>
/// 新增快递单-初始空页面
/// </summary>
[HttpGet]
[AccessGroup(GroupKey = "RMAExpress_Add")]
public ActionResult Add(RMAExpressDetailInfo model)
{
GetSalesList();//为前台下拉框绑定值做准备
model.Model.SaleExecutor = userId;//传默认值给前台
return View(model);
}
在提交的时候,会自动把值映射到对应的模型中,当然特殊需要的时候可以通过Jquery单独获取选中值
var saleUserId = $("#Model_SaleExecutor").find("option:selected").val();//找到下拉框的选中的Value
Ajax联动动态为@Html.DropDownListFor赋值
<script type="text/javascript"> function getTeams(deptCode) { $.ajax({ url: "@Url.Action("GetTeams","User")",data: { departmentCode: deptCode },dataType: "json",type: "POST",error: function () { alert("An error occurred."); },success: function (data) { var items = ""; $.each(data,function (i,item) { items += "<option value=\"" + item.Value + "\">" + item.Text + "</option>"; }); $("#@Html.IdFor(m => m.TeamId)").html(items); } }); } $(document).ready(function () { $("#@Html.IdFor(m => m.DepartmentCode)").change(function () { var deptCode = $("#@Html.IdFor(m => m.DepartmentCode)").val(); getTeams(deptCode); }); }); </script>
根据部门ID(deptCode)获取该部门下的组(Team)。
Controller:
User的GetTeams(string departmentCode)返回一个集合,通过
$.each(data,item)进行循环添加。
ASP.NET MVC 让@Html.DropDownList显示默认值
一、View代码
- @using (Ajax.BeginForm("Edit", new AjaxOptions() { OnSuccess = "afterEdit" }))
- {
- @Html.HiddenFor(model => model.ID)
- @Html.HiddenFor(model => model.isUsed)
- <div>
- <tablealign="center">
- <tr>
- <td>@Html.Label("评论人职务:")</td>
- <td>@Html.DropDownList("YzPositionCriticsID", ViewData["CriticsPositionType"] as SelectList)</td>
- </tr>
- <tr>
- <td>@Html.Label("被评论人职务:")</td>
- <td>@Html.DropDownList("YzPositionEvaluationID", ViewData["EvaluationPosition"] as SelectList)</td>
- </tr>
- <tr>
- <td>@Html.Label("权重:")</td>
- <td>@Html.TextBoxFor(model => model.Weight)</td>
- </tr>
- </table>
- </div>
- }
二、Controller代码
- public ActionResult Edit(string id)
- {
- //6.1根据ID从数据库中查出要修改的相关数据
- Guid Gid = new Guid(id);
- Model.DTO.YzWeightEntityDTO weight = weightBLL.LoadEnities(u => u.ID == Gid).FirstOrDefault().ToDto();
- //6.2查询YzPositionEntity实体的数据
- List<Model.DTO.YzPositionEntityDTO> positionList = positionBLL.LoadEnities().ToList().Select(s => s.ToDto()).ToList();
- //6.3将YzPositionEntity的数据封装到 SelectList中,制定要生成下拉框选项的value和text属性
- SelectList selList1 = new SelectList(positionList, "ID", "PositionType", weight.YzPositionCriticsID);
- SelectList selList2 = new SelectList(positionList, "ID", "PositionType",weight.YzPositionEvaluationID);
- //6.4把生成的集合放到ViewData中
- ViewData["CriticsPositionType"] = selList1;
- ViewData["EvaluationPosition"] = selList2;
- return View(weight);
- }
在这里需要注意的是:SelectList selList1 = new SelectList(list,"","",***),这里的***就是下拉框加载时候要显示的默认值。
最后的显示效果如下:
总结:
在MVC的学习过程中,总会时不时遇到一些小问题,需要转个弯才能找到答案。在解决问题的过程中经验真的很重要,它能告诉你一个问题的突破点在什么地方;除此之外,熟练的使用api文档也是解决问题的重要途径;最后,真的发现解决问题是提高自己学习兴趣的不接动力。
ASP.NET MVC4.0 DropDownListFor 设置默认选项
public ActionResult Edit(long id = 0)
{
Post post = db.Post.Find(id);
if (post == null)
{
return HttpNotFound();
}
ViewBag.content = post.content;
ViewBag.cates = new SelectList(db.PostCate, "id", "name", post.cate_id);
ViewBag.updetp = new SelectList(db.Bank, "id", "name", post.updept_id);
var ds = from d in db.Department
where d.updep == post.updept_id
select new { d.id, d.name };
ViewBag.depts = new SelectList(ds, "id", "name", post.dept_id);
return View("~/Views/Post/Edit.cshtml", post);
}
<p>
<label for="cate_id">类别:</label>
@Html.DropDownListFor(model=>model.cate_id, ViewBag.cates as IEnumerable<SelectListItem>)
@Html.ValidationMessage("cate_id", "请选择公告类型")
</p>
<p>
<label for="updetp">分行:</label>
@Html.DropDownListFor(model=>model.updept_id, ViewBag.updetp as IEnumerable<SelectListItem>, String.Empty, new { style = "width:180px" })
@Html.ValidationMessage("updept_id", "请选择分行")
</p>
<p>
<label for="dept_id">机构:</label>
@Html.DropDownListFor(model => model.dept_id, ViewBag.depts as IEnumerable<SelectListItem>,string.Empty, new { style = "width:180px" })
@Html.ValidationMessage("dept_id", "请选择发布机构")
</p>
关键点在于ViewBag.XX不能与字段名同名。否则无法设置默认值。应该是因为冲突。
例如:
@Html.DropDownListFor(model => model.dept_id, ViewBag.depts as IEnumerable<SelectListItem>,string.Empty, new {})
不能写成:
@Html.DropDownListFor(model => model.dept_id,ViewBag.dept_id as IEnumerable<SelectListItem>,string.Empty, new {})
关于@ Html.DropDownList用于设置默认值的方法和怎么设置dropdownlist默认值的介绍现已完结,谢谢您的耐心阅读,如果想了解更多关于@Html.DropDownListFor 下拉框绑定(选择默认值)、Ajax联动动态为@Html.DropDownListFor赋值、ASP.NET MVC 让@Html.DropDownList显示默认值、ASP.NET MVC4.0 DropDownListFor 设置默认选项的相关知识,请在本站寻找。
本文标签: