简体   繁体   English

在ASP.Net MVC3中使用jQuery动态填充下拉列表

[英]Dynamically populate the drop-down using jQuery in ASP.Net MVC3

I have two models: 我有两个模型:

public class ProfessorModels
{
    public string FullName { get; set; }
    public int ID { get; set; }
}

and

public class ClassModels
{
    public int ID { get; set; }
    public string Professor { get; set; }
    public decimal Name { get; set; }
}

in my View there is a form to add the class: 在我的视图中,有一个添加类的表单:

@model MvcApp.Models.ClassModels

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>ClassModels</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.Name)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Name)
            @Html.ValidationMessageFor(model => model.Name)
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

I would like to add a drop-down menu to the Class View, which lists all the available professors. 我想在班级视图中添加一个下拉菜单,其中列出了所有可用的教授。 Professors are in db, and I can easily make a call to db from controller and load all professors in to some list/array. 教授在db中,我可以轻松地从控制器调用db并将所有教授加载到某个列表/数组中。 I need help with how to populate the drop-down with professors using jQuery . 我需要有关如何使用jQuery的教授填充下拉列表的帮助。

In your controller: 在您的控制器中:

    [HttpGet]
    public virtual JsonResult LoadInfo()
    {
        var query = _repository.GetInformation(); //Here you return the data. 
        return Json(query, JsonRequestBehavior.AllowGet);
    }

Then in your view: 然后在您看来:

<select id="info"></select>

Then you load the drop down using jQuery 然后使用jQuery加载下拉列表

function LoadInfo() {

    $.getJSON("@Url.Action(MVC.ControllerName.MethodName())", null,
        function (data) {

            $("#info").empty();

            $.each(data, function () {
                $("#info").append($("<option />").val(this.Id).text(this.Name));
            });

        });
}

This assumes that Id and Name are properties of your object. 假定Id和Name是对象的属性。 You could use ID and FullName depending on which drop down you're loading. 您可以使用ID和FullName,具体取决于要加载的下拉列表。 I also use T4MVC to get the different method names. 我也使用T4MVC获得不同的方法名称。

Hope this helps, 希望这可以帮助,

Have an action method which returns a List of Proffessors 有一个返回方法列表的action方法

public ActionResult GetProfessors()
{
  var professorList=repo.GetProfessors(); //get list of professor object
  return Json(professorList,JsonRequestBehavior.AllowGet);
}

Now in your View, Have a DropDown 现在在您的视图中,有一个下拉菜单

<select id="listProfessors"></select>

Use jQuery ajax to load the data to this element on the document ready event. 使用jQuery ajax将数据加载到文档ready事件中的该元素上。 Add the below script in your view. 在您的视图中添加以下脚本。

<script type="text/javascript">
  $(function(){
    var items="";
    $.getJSON("@Url.Action("GetProfessors","YourControllerName")",function(data){

        $.each(data,function(index,item){
           items+="<option value='"+item.ID+"'>"+item.FullName+"</option>";
        });
        $("#listProfessors").html(items);
    });

  });    
</script>

Assuming your Controller name is YourController and you have jQuery loaded into this page properly. 假设您的Controller名称为YourController并且您已将jQuery正确加载到此页面中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在不使用任何插件的情况下使用jquery在ASP.Net MVC3中拖放表行? - How to drag and drop table row in ASP.Net MVC3 using jquery without using any plugin? 使用JavaScript jQuery使用其余选项动态填充4个下拉列表 - Dynamically populate 4 drop-down lists with the remaining options with JavaScript jQuery jQuery 使用 Javascript Object Literal 基于另一个下拉选项填充下拉选项 - jQuery populate drop-down options based on another drop-down option using Javascript Object Literal 如何基于第一个下拉选择值绑定第二个下拉菜单ASP.NET MVC3 - How to Bind the second dropdown based on the first drop down selected value ASP.NET MVC3 无法从asp.net页获得要在jquery令牌输入下拉列表中使用的结果 - cannot get results from an asp.net page to be used in the jquery token input drop-down 使用对象(JavaScript或jQuery)填充选择下拉列表 - Populate select drop-down using object (JavaScript or jQuery) 如何使用 jquery 或 ZDEZZB9ED78E2E9E72Z 核心 mvc 从 asp.net 的下拉列表中自动 select 值? - How to automatically select value from drop down list in asp.net core mvc using jquery or javascript? 如何在禁用的 asp.net 下拉菜单中添加警报? - How to add alert on disabled asp.net drop-down? ASP.Net MVC 3下拉列表 - ASP.Net MVC 3 Drop Down List 使用ASP.NET MVC3的Dynatree - Dynatree using asp.net mvc3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM