简体   繁体   English

在 Razor 中渲染 JS 函数的返回值

[英]Rendering returned value from JS function in Razor

I have a page on my site that displays a list of members.我的网站上有一个显示成员列表的页面。 I'm using .NET Core MVC and Razor to render my views.我正在使用 .NET Core MVC 和 Razor 来呈现我的视图。

I'm storing the members' registration date as UTC, and would like that date/time converted to the user's local timezone when they view the page.我将成员的注册日期存储为 UTC,并希望在他们查看页面时将该日期/时间转换为用户的本地时区。

I'm using javascript to do this:我正在使用 javascript 来执行此操作:

<script>
    function GetLocalDateTime(utcDate) {
        var localDate = new Date(utcDate);

        console.log(localDate.toString());

        return localDate.toString();
    }
</script>


....


@foreach (var member in Model.Items)
{
    <tr>
        ...
        <td>
            <script>
                GetLocalDateTime('@(member.CreatedDate.ToString("MM/dd/yyyy hh:mm:ss tt UTC"))');
            </script>
        </td>
    </tr>
}

The problem is that the return value from the javascript function doesn't get displayed in my table.问题是 javascript 函数的返回值没有显示在我的表中。 But the function is being called, the converted date values are logging in the console.但是正在调用该函数,转换后的日期值正在控制台中记录。

What am I doing wrong here?我在这里做错了什么?

If you want show the date,you need to update the dom rather than just return the local date如果要显示日期,则需要更新 dom 而不是只返回本地日期

modify return localDate.toString();修改return localDate.toString(); to document.write( localDate.toString());document.write( localDate.toString()); 在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有没有办法从 JS function 更新返回值? - Is there a way to update a returned value from a JS function? 转换js函数返回的值 - converting value returned from js function 从react中的handleclick()函数内部返回时,返回值未呈现 - return value is not rendering when returned from inside the handleclick() function in react JS中返回的多个值/如何从函数外部访问返回值数组? - Multiple value returned in JS/How to access an array of returned value from outside the function? 从函数获取返回值 - Get returned value from function Node js函数需要返回值,但是值来自回调,因此无法返回 - Node js function requires a return value but value comes from callback so cannot be returned 从JS / Node模块获取返回值 - Getting returned value from a JS / Node Module Firebase 返回的值未在 ejs 中呈现 - Firebase returned value not rendering in ejs 从函数返回的Stange值(使用popcorn.js,angular和jade) - Stange value being returned from a function ( using popcorn.js, angular, and jade ) React js app - 使用 class 组件中 function 的返回值设置功能组件的 state (错误:未定义) - React js app - setting functional component's state with a returned value from function in class component (error: undefined)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM