Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 根据“我的模型”中的值创建动态复选框_C#_Jquery_Asp.net Mvc_Json_Asp.net Mvc 4 - Fatal编程技术网

C# 根据“我的模型”中的值创建动态复选框

C# 根据“我的模型”中的值创建动态复选框,c#,jquery,asp.net-mvc,json,asp.net-mvc-4,C#,Jquery,Asp.net Mvc,Json,Asp.net Mvc 4,我在MVC 4中工作,在我看来,我编写此代码是为了生成复选框: <tr class="formSectionField clearfix"> <td> <label id="lblStoreList" class="formSectionLabel">Title:</label> </td> <td> <div class="txtInput input323">

我在MVC 4中工作,在我看来,我编写此代码是为了生成复选框:

<tr class="formSectionField clearfix">
   <td>
      <label id="lblStoreList" class="formSectionLabel">Title:</label>
   </td>
   <td>
     <div class="txtInput input323">
        @Html.CheckBoxFor(m => m.StoreList, new SelectList(Enumerable.Empty<SelectListItem>()))
     </div>
   </td>
</tr>
function BindTitle() {
    $.ajax({
        "url": "/Admin/GetTitleList/",
        "type": "get",
        "dataType": "json",
        "success": function (data) {
            var items = $('#TitleId');
            items.empty();
            $.each(data, function (i, drdData) {
                items.append($('<checkbox/>', { value: drdData.Value, html: drdData.Text }));
            });
        }
    });
}

标题:
@CheckBoxFor(m=>m.StoreList,new SelectList(Enumerable.Empty()))
最初,我只是告诉MVC框架,它将有一个checbox,但我希望根据模型中包含的值设置复选框的编号

我如何才能做到这一点?

下面是使用AJAX的JS代码,它将生成我的动态复选框:

<tr class="formSectionField clearfix">
   <td>
      <label id="lblStoreList" class="formSectionLabel">Title:</label>
   </td>
   <td>
     <div class="txtInput input323">
        @Html.CheckBoxFor(m => m.StoreList, new SelectList(Enumerable.Empty<SelectListItem>()))
     </div>
   </td>
</tr>
function BindTitle() {
    $.ajax({
        "url": "/Admin/GetTitleList/",
        "type": "get",
        "dataType": "json",
        "success": function (data) {
            var items = $('#TitleId');
            items.empty();
            $.each(data, function (i, drdData) {
                items.append($('<checkbox/>', { value: drdData.Value, html: drdData.Text }));
            });
        }
    });
}
函数BindTitle(){
$.ajax({
“url”:“/Admin/GetTitleList/”,
“类型”:“获取”,
“数据类型”:“json”,
“成功”:功能(数据){
var项目=$(“#TitleId”);
items.empty();
$。每个(数据、函数(i、drdData){
append($('',{value:drdData.value,html:drdData.Text}));
});
}
});
}

希望我已经向您说明了这一点,无论如何,如果您想要进一步的建议,请随时询问

查看asnswer,我认为MVC 3和MVC 4中的情况是相同的。@Daniele谢谢,但使用ViewBag获取特定属性的模型数据,然后使用@foreach(…){}