我需要在 mvc 页面的脚本部分填充一个对象,当前该对象如下所示:
<script>
var disabledDays = ["9-30-2011","2-24-2010","2-27-2010","2-28-2010","3-3-2010","3-17-2010","4-2-2010","4-3-2010","4-4-2010","4-5-2010"];
现在我尝试将 DateTime 对象数组从 Controller 传递到 View ,但在执行此操作之前将其转换为字符串。像这样的事情:
<Controller>
var blockedDates = new List<string>();
foreach (DateTime closeDate in dealershipInfo.ClosedDates)
blockedDates.Add(closeDate.ToString());
ViewBag.BlockedDates = blockedDates;
但它绝对不适合我。实现这种结果的正确方法是什么。
最佳答案
public ActionResult Index()
{
var blockedDates = dealershipInfo.ClosedDates.Select(x => x.ToString()).ToList();
return View(blockedDates);
}
在 View 中:
@model IEnumerable<string>
...
<script type="text/javascript">
var disabledDays = @Html.Raw(Json.Encode(Model));
</script>
关于jquery - MVC : Need to pass an list or array of strings from controller to view,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7587166/