您无法发送text/html
and application/json
对于单个请求。您可以发送其中任何一个。
如果您从控制器返回 PartialView,则内容类型将设置为text/html
。但您仍然可以将 JSON 呈现为部分视图的一部分。
public ActionResult RenderView(int code,long foundStudent,int sessionName)
{
var months = //get some months
var myMonths= (from w in months
select new
{
w.Month
});
ViewBag.Months=myMonths;
return PartialView("MonthwiseFees");
}
然后在你的局部视图中
<script>
var [email protected](JSON.Encode(ViewBag.Months)
</script>
jQuery
success: function(data) {
$('#maindiv').html('')
$('#maindiv').html(data)
$.each(myJSArray, function(index, item) {
//myJSArray will be available in window scope now
})
}
Update:基于更新的问题
控制器
public ActionResult RenderView(int code,long foundStudent,int sessionName)
{
var months = //get some months
var myMonths= (from w in months
select new
{
w.Month
});
return PartialView("MonthwiseFees",myMonths);
}
当我通过时,这将选中所有复选框true
对于 Html.CheckBox。这需要通过模型和月份数组来确定
@model List<string>
@using System.Globalization;
@{
Layout = null;
string[] months =DateTimeFormatInfo.CurrentInfo.MonthNames;
var countMonths=months.Count();
int x = 1;
}
<div>
@for (int no=0;no<countMonths-1;no++)
{
<tr>
<td>
@x
</td>
<td>
@months[no]
</td>
<td>
//determine true/false by comparing months and Model
@Html.CheckBox(" " + @x, model.Contains(months[x]), new { @class="check",id="check"})
</td>
</tr>
x = x + 1;
}
</div>
jQuery