如何设置dropdownlistfor默认值状态 [英] How to set state of dropdownlistfor default value

查看:83
本文介绍了如何设置dropdownlistfor默认值状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何设置为默认值的DropDownList 的状态通过EDMX(而不是手动创建的)使用自动生成的模型时,存储在数据库中编辑视图之一。

How to set state of dropdownlist for default value to the one stored in database in edit view when using auto generated models via edmx (not created manually).

下面是我的code:

查看:

 @Html.DropDownListFor(model => model.site_id, new SelectList(ViewBag.site_id, "id", "name"), "Select Any", new {@class="form-control",required="required",autofocus="autofocus" })

控制器:

  ViewBag.site_id = dbContext.axi_sites;

一直在寻找相当长一段时间的解决方案,没有比赛的确切要求。

Been looking for the solution for quite a while, none match to the exact requirement.

推荐答案

我们为什么要打扰。我们可以使用jQuery了。

Why should we bother. We can use jquery too.

在code添加的jQuery 1.10.3。
和code低于你的页面,你想选择dropdownlistfor值

add jquery 1.10.3 in your code. and code below in your page where you want dropdownlistfor selected value

在View部分写

<script  type="text/JavaScript">
   $(document).ready(function () {
     $('#site').val(@Model.site_id);
  });
</script>

@Html.DropDownListFor(model => model.site_id, new SelectList(ViewBag.site_id, "id", "name"), "Select Any", new {@class="form-control",required="required",autofocus="autofocus",id="site" })

这篇关于如何设置dropdownlistfor默认值状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆