我在 How to use javascript variables in C# and vice versa
中遇到问题:我将这个 Model
传递给 View :
public List<Tache> Get_List_Tache()
{
Equipe _equipe = new Equipe();
List<Tache> liste_initiale = _equipe.Get_List_tache();
return liste_initiale;
}
这是一个对象列表 Tache
,我想在其中使用它的三个字段 Tache_description
、Begin_date
和 End_date
.
在我的 JavaScript 代码中我有这个函数并且它工作正常:
<script>
$(document).ready(function () {
var date = new Date();
var d = date.getDate();
var m = date.getMonth();
var y = date.getFullYear();
$('#calendar').fullCalendar({
theme: true,
header: {left: 'prev,next today',center: 'title',right: 'month,agendaWeek,agendaDay'},
editable: true,
events: [
@foreach (var m in Model.Get_List_Tache())
{
@:{title : @m.Tache_description , start: @m.Begin_date , end : @m.Begin_date }
}
]
});
});
</script>
数组events
的值只是为了测试,我需要用Model
的值填充events
。对于这样的每个元素:title = Tache_description
、start = Begin_date
和 end = End_date
。
那么我该如何完成这个任务呢?有什么建议吗?
最佳答案
试试这个,
foreach (var item in YourList)
{
events: [{ title: '@item.title', start: '@item.start', end: '@item.end'}]
}
因此,在此代码中只需替换您的模型实体的名称。
关于c# - 在 asp.net mvc 应用程序中从 Javascript 访问 C# 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17671385/