开发者

MVC: Need to pass an list or array of strings from controller to view

开发者 https://www.devze.com 2023-04-08 21:09 出处:网络
I need to populate an object in the script section of my mvc page, currently the object looks like this:

I need to populate an object in the script section of my mvc page, currently the object looks like this:

<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"];

Now I am trying to pass an array of DateTime objects to the view from the controller, but converting it to strings before I do that. Something like this开发者_JAVA技巧:

<Controller>
var blockedDates = new List<string>();
            foreach (DateTime closeDate in dealershipInfo.ClosedDates)
                blockedDates.Add(closeDate.ToString());
            ViewBag.BlockedDates = blockedDates;

But definitely it is not working for me. What will be the proper way to achieve this kind of a result.


public ActionResult Index()
{
    var blockedDates = dealershipInfo.ClosedDates.Select(x => x.ToString()).ToList();
    return View(blockedDates);
}

and in the view:

@model IEnumerable<string>
...
<script type="text/javascript">
    var disabledDays = @Html.Raw(Json.Encode(Model));
</script>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号