在MVC 4中使用数据库中的数据填充单选按钮,而不使用模型

fill radiobutton with data from data base in mvc 4 without using model

本文关键字:单选按钮 模型 填充 MVC 数据库 数据      更新时间:2023-09-26

控制器中的代码:此操作从数据库中获取数据并使用此数据填充(classlist)

public ActionResult GetClassListForTeacher()
        {
            List<string> classlist = null;
            ProfileEntities pe= new ProfileEntities();
            string uID = HttpContext.Session["NID"].ToString();
            int year = 2011;
            //int year = DateTime.Now.Year;
            int session = set_session();
            if (session == 1)//teacher
            {
                classlist = (from c in pe.CLASSES
                                 join lc in pe.LESSON_CARDS
                                 on new { EDUSTG_CODE = c.EDUSTG_CODE, STATISTICAL_CODE = c.STATISTICAL_CODE, FIRST_YEAR = c.FIRST_YEAR,
                                 GRD_CODE = c.GRD_CODE , OCF_CODE = c.OCF_CODE , CLS_SN = c.CLS_SN} 
                                 equals new { EDUSTG_CODE  = lc.EDUSTG_CODE, STATISTICAL_CODE = lc.STATISTICAL_CODE_BELONGS_TO, FIRST_YEAR = lc.FIRST_YEAR,
                                 GRD_CODE = lc.GRD_CODE , OCF_CODE = lc.OCF_CODE_BELONGS_TO , CLS_SN = lc.CLS_SN}
                                 where (lc.NID == uID && lc.FIRST_YEAR_HAVE == year)
                                 select c.NAME
                                ).ToList();
            }
            if (session == 2)//student
            {}
            return Json(classlist, JsonRequestBehavior.AllowGet);
        }

view

中的代码
<div class="dialog-fluid" id="MoreOptionModal" style="display: none; height: 300px; overflow: scroll;" title="Options">
    <div class="row-fluid">
        <div class="block-fluid">
            <div class="row-form">
                <div class="span12">
                    <table style="border: 0;">
                        <tr>        
                            <td style="border: 0;">@Html.RadioButton("type","Friendes",false,new { id = "Friendes"})</td>               
                            <td style="border: 0;"><label>All Friendes</label></td>
                        </tr>
                        <tr>
                            <td style="border: 0;">@Html.RadioButton("type","Teachers",false,new { id = "Teachers"})</td>               
                            <td style="border: 0;"><label>All Teachers As Friends</label></td>
                        </tr>
                        <tr>
                            <td style="border: 0;"></td>
                            <td style="border: 0;">@Html.RadioButton("type","Same School",false,new { id = "Same_School" })</td>
                            <td style="border: 0;"><label>Same School</label></td>
                        </tr>
                        <tr>
                            <td style="border: 0;"></td>
                            <td style="border: 0;">@Html.RadioButton("type","Other School",false,new { id = "Other_Schools"})</td>
                            <td style="border: 0;"><label>Other School</label></td>
                        </tr>
                        <tr>
                            <td style="border: 0;">@Html.RadioButton("type","Schoolmates",false,new { id = "Schoolmates"})</td>               
                            <td style="border: 0;"><label>All Students As Friends</label></td>
                        </tr>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

我想在最后一个单选按钮(同学)之后添加单选按钮列表这是js

中的代码
 $("#MoreOptionModal").dialog({
        autoOpen: false,
        modal: true,
 });
    $("#options").live("click", function () {
        debugger;
        $("#MoreOptionModal").dialog('open');
        $.ajax({
            url: "/Mail/GetClassListForTeacher",
            type: "POST",
            datatype: "json",
            data: { },
            traditional: true,
            success: function (result) {
            }
            });
  });

问题是我的视图中已经有了模型我从数据库中检索数据我想显示我的视图

将此代码添加到success函数中,当然您必须根据您的需求调整解决方案,但基本上这是一个变通方法。

$.each(result, function(i, name){
    var trToAppend="
    <tr>
       <td style="border: 0;"><input type='radio' name='type' id='" + name + "'/></td>               
       <td style="border: 0;"><label>" + name + "</label></td>
    </tr>
    ";
    $('table').append(trToAppend);
});

js代码

$("#options").live("click", function () {
        debugger;
        $("#MoreOptionModal").dialog('open');
        $.ajax({
            url: "/Mail/GetClassListForTeacher",
            type: "POST",
            datatype: "json",
            data: { },
            traditional: true,
            success: function (result) {
                $("#ForTest").val(result);
                var d = $("#ForTest").val().split(",");
                for (var i = 0; i < d.length; i++) {
                    //alert(d[i])
                    var trToAppend="<tr><td><input type='radio' name='type' id='" + d[i] + "'/></td><td><label>" + d[i] + "</label></td></tr>";
                    $("#MyTest").append(trToAppend);
                }
            }
            });
  });

我在view

中添加了这一行
<input id="ForTest" type="hidden" />

我将列表拆分然后添加HTML