如何将项目添加到SelectList中的下拉列表(Enumerable.Empty <selectlistitem>()格式

时间:2015-12-04 06:34:08

标签: c# asp.net asp.net-mvc asp.net-mvc-4 razor

我有一个像这样的下拉列表

@Html.DropDownListFor(model => model.si_sec_id, new SelectList(Enumerable.Empty<SelectListItem>(), "Value", "Text"), "Select a Section", new { id = "ddlSection" })

就像是因为这个

<script type="text/javascript">
    $(document).ready(function () {
        $("#ddlGrade").change(function () {
            var id = $(this).val();
            $.getJSON("../Employee/PopulateDetails", { id:id},
                   function (marksData) {
                       var select = $("#ddlSection");
                       select.empty();
                       select.append($('<option/>', {
                           value: 0,
                           text: "Select a Section"
                       }));
                       $.each(marksData, function (index, itemData) {
                           select.append($('<option/>', {
                              value: itemData.Value,
                               text: itemData.Text
                           }));
                       });
                   });
        });
    });

和JSON

public JsonResult PopulateDetails(string id)
    {

        List<Models.Section> a = new List<Models.Section>();
        Models.ModelActions Ma = new ModelActions();

        a = Ma.getSection(id);

        var marksData = a.Select(c => new SelectListItem()
        {
            Text = c.sec_name,
            Value = c.sec_id.ToString(),
        });

        return Json(marksData, JsonRequestBehavior.AllowGet);
    }

现在如何在回发时以该格式向下拉列表中添加初始值?我需要它用于我的搜索功能。评论非常感谢

编辑:

查看:

 <legend>CreateStudent</legend>

            Full Name:
            @Html.TextBox("searchTerm", null, new { id = "txtSearch" })
            <input type="submit" value="search" name="submitbutton" />


            <div class="editor-label">
                @Html.LabelFor(model => model.si_id)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(model => model.si_id, new { @readonly = "readonly" })
                @Html.ValidationMessageFor(model => model.si_id)
            </div>

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

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

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

      <div class="editor-label">
        @Html.LabelFor(model => model.si_gl_id)
            </div>
            <div class="editor-field">
        @Html.DropDownListFor(model => model.si_gl_id, new  SelectList(Model.GradeLevel,"gl_id","gl_name"),"Select Grade Level", new { id = "ddlGrade" })
        @Html.ValidationMessageFor(model => model.si_gl_id)
            </div>

            <div class="editor-label">
        @Html.LabelFor(model => model.si_sec_id)
            </div>
            <div class="editor-field">
                @Html.DropDownListFor(model => model.si_sec_id, new SelectList(Enumerable.Empty<SelectListItem>(), "Value", "Text"), "Select a Section", new { id = "ddlSection" })
                @Html.ValidationMessageFor(model => model.si_sec_id)
         </div>
            <p>
                <input type="submit" value="Create"    name="submitbutton"   />
            </p>
        </fieldset>



            <p>
                <input type="submit" value="Create" name="submitbutton" />
            </p>

控制器

        [HttpPost]
        public ActionResult RegisterStudent(CreateStudent Create, string submitbutton, string searchTerm)
    {

         acgs_qm.Models.ModelActions Ma = new acgs_qm.Models.ModelActions();
        List<CreateStudent> stud = new List<CreateStudent>();

        switch (submitbutton)
        {
            case "search":
                ModelState.Clear();

                var model = new CreateStudent
                {
                    GradeLevel = Ma.getGrade(),
                    //Guardian = Ma.getGuardian(),
                    si_id = Ma.getStringval(searchTerm,"si_id","student_info_tb","si_fullname"),
                    si_fname = Ma.getStringval(searchTerm, "si_fname", "student_info_tb", "si_fullname"),
                    si_mname = Ma.getStringval(searchTerm, "si_mname", "student_info_tb", "si_fullname"),
                    si_lname = Ma.getStringval(searchTerm, "si_lname", "student_info_tb", "si_fullname"),
                    si_gender = Ma.getStringval(searchTerm, "si_gender", "student_info_tb", "si_fullname"),


                };

                return View("RegisterStudent",model);
            case "Create":
                if (ModelState.IsValid)
                {
                    Ma.insertStudent(Create);
                }
                Create.GradeLevel = Ma.getGrade();
                Create.si_id = Ma.getcode("student_info_tb", "si_id", "1");
                return View(Create);
            default:
                return View(Create);
        }

    }

2 个答案:

答案 0 :(得分:0)

按如下方式更改您的实施:

$.getJSON("../Employee/PopulateDetails", { id:id},
                   function (marksData) {
                       var $select = $("#ddlSection");
                       $select.empty();
                       $select.append('<option value=' + '0' + '>' + 'Select a Section' + '</option>');
                       $.each(marksData, function (index, itemData) {
                           $select.append('<option value=' + itemData.Value + '>' + itemData.Text + '</option>');
                       });
                   });

答案 1 :(得分:0)

试试这个

$("#ddlSection").change(function () {
    var Id = this.value;           
    if (Id != 0) {
    $.ajax({
          type: "POST",                  
          url: "/Employee/PopulateDetails",
          data: JSON.stringify({ Id: Id }),
          dataType: "text",
          contentType: "application/json; charset=utf-8",
          processData: false,
          success: function (data) {
                        $("#ddlSection").empty();
                        $('#ddlSection').append("<option value='0'>Select Selection...</option>");
                        var yourArray = $.parseJSON(data);
                        if (yourArray != null) {
                            for (var i = 0; i < yourArray.length; i++) {
                                $('#ddlSection').append("<option value='" + yourArray[i].YourFiledName + "'>" + yourArray[i].YourFiledName + "</option>");
                            }                              
                        }
                    },
                    error: function (response) {
                        if (response != 1) {
                            alert("Error!!!!");
                            location.reload();
                        }
                    }
                });
            }
            else {
                alert("Please Select Any Value Name....");
            }
        });