如何将刀片型号为JS对象剃刀刮循环? [英] How to turn Razor Model into JS object in a Razor For Loop?

查看:145
本文介绍了如何将刀片型号为JS对象剃刀刮循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个循环剃刀:

  @foreach(在Model.Users VAR用户)
        {
        < p =班活动文本>活动:@ user.LastActive< / P>
        }

我刚刚安装moment.js格式化使用JS这个日期时间()日期。

我怎样才能通过剃刀模型转换成javascript函数?我有一个JS视图模型此页,我只是试图避免序列化整个模型只是因为我需要一些JS应用到单个字段。我的ViewModel如何站在现在:

 <脚本类型=文/ JavaScript的>
    $(文件)。就绪(ko.applyBindings(新SubjectVm()));
< / SCRIPT>


解决方案

我想换行日期文本在另一个跨度为以后处理:

 < p类=活动文本>活动:其中,跨度类=主动文本日期> @ user.LastActive< / SPAN>< /差异无显着

然后循环并应用格式,里面document.load:

 <脚本>
    $(文件)。就绪(函数(){
        $(主动文本日期)。每个(函数(){
            VAR日期= $(本)的.text();
            VAR格式化=时刻(日期).calendar();
            $(本)的.text(格式化);
        });
    });
< / SCRIPT>

I have a Razor for loop:

@foreach (var user in Model.Users)
        {
        <p class="active-text">Active: @user.LastActive</p>
        }

I've just installed moment.js to format this DateTime() date using js.

How can I pass the Razor model into a javascript function? I do have a JS viewmodel for this page, I'm just trying to avoid serializing the entire Model just because I need to apply some JS to a single field. How my viewModel stands right now:

<script type="text/javascript">
    $(document).ready(ko.applyBindings(new SubjectVm()));
</script>

解决方案

I would wrap the date text in another span for later processing:

<p class="active-text">Active: <span class="active-text-date">@user.LastActive</span></p>

Then loop through and apply the formatting, inside document.load:

<script>
    $(document).ready(function() {
        $(".active-text-date").each(function() {
            var date = $(this).text();
            var formatted = moment(date).calendar();
            $(this).text(formatted);
        });
    });
</script>

这篇关于如何将刀片型号为JS对象剃刀刮循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆