getElementById()。值返回null asp.net MVC

发布于 2025-02-06 09:39:30 字数 4978 浏览 3 评论 0原文

我是ASP.NET MVC的新手,我正在尝试为图表创建过滤器。我已经使用了几个按钮。

@using Common
@model DateModel
<div>
    <canvas id="myChart"></canvas>
</div>
<div>
    <p>

    </p>
</div>


<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script>


    var dates = [@Html.Raw(Json.Encode(ViewBag.dates))];
    var DeletedCount = [@Html.Raw(Json.Encode(ViewBag.DeletedCount))];
    var ApprovedCount = [@Html.Raw(Json.Encode(ViewBag.ApprovedCount))];
    var ClosedCount = [@Html.Raw(Json.Encode(ViewBag.ClosedCount))];
    var OngoingCount = [@Html.Raw(Json.Encode(ViewBag.OngoingCount))];
    var NewCount = [@Html.Raw(Json.Encode(ViewBag.NewCount))];
    var ToBeAccepetdCount = [@Html.Raw(Json.Encode(ViewBag.ToBeAccepetdCount))];


    const data = {
        labels: dates[0],
        datasets: [{
            label: 'Deleted',
            backgroundColor: 'rgb(255, 99, 132)',
            borderColor: 'rgb(255, 99, 132)',
            data: DeletedCount[0],
        },
            {
                label: 'Approved',
                backgroundColor: 'rgb(0,192, 0)',
                borderColor: 'rgb(0,192, 0)',
                data: ApprovedCount[0],
            },
            {
                label: 'Closed',
                backgroundColor: 'rgb(128, 128, 128)',
                borderColor: 'rgb(128,128, 128)',
                data: ClosedCount[0],
            },
            {
                label: 'Ongoing',
                backgroundColor: 'rgb(0, 32, 255)',
                borderColor: 'rgb(0, 32, 255)',
                data: OngoingCount[0],
            },
            {
                label: 'New',
                backgroundColor: 'rgb(80, 208, 255)',
                borderColor: 'rgb(80, 208, 255)',
                data: NewCount[0],
            },
            {
                label: 'To be Accepted',
                backgroundColor: 'rgb(90, 255, 128)',
                borderColor: 'rgb(90, 255, 128)',
                data: ToBeAccepetdCount[0],
            },

        ]
    };

    const config = {
        type: 'line',
        data: data,
        options: {
            responsive: true,
            plugins: {
                legend: {
                    position: 'top',
                },
                title: {
                    display: true,
                    text: 'Tasks'
                }
            },

            scales: {
                y: {
                    beginAtZero: true
                    ,
                    title: {
                        display: true,
                        text: 'Count'
                    }
                }
            }

        },

    };
</script>
<script>
    const myChart = new Chart(
        document.getElementById('myChart'),
        config
    );
</script>

<div align="center">
    <form method="POST">
        <button type="submit" class="btn btn-light" onclick="Today()">Today</button>
        <button type="submit" class="btn btn-light">Last 7 Days</button>
        <button type="submit" class="btn btn-light">This Month</button>
        <button type="submit" class="btn btn-light">Last Month</button>
        <script>
            function Today() {
                document.getElementById('sdate').value = '2022-02-01';
                document.getElementById('edate').value = @DateTime.Now.ToString("yyyy-MM-dd");;
                console.log(document.getElementById('sdate').value);
                console.log(document.getElementById('edate').value);
            }
        </script>
    </form>
 
</div>


<hr />

<div class="row">

    <form method="POST">
        <label for="sdatelbl">Start Date</label>
        <input type="text" class="form-control" placeholder="@ViewBag.Sdate" id="sdate" name="sdate" value="" size="10" maxlength="10" />
        <label for="edatel">End Date</label>
        <input type="text" class="form-control" [email protected] id="edate" name="edate" value="" size="15" maxlength="30" />
        <br>


        <input type="submit" class="btn btn-primary" onclick="" value="Refresh">
    </form>
</div>

今天的按钮

当我单击此单击时,我想更改 sdata 值另一种用作cutom滤镜的表单。但是,当我单击按钮时,它在控制台中打印正确的值,但在家庭控制器中返回null。

控制台输出 homecontroller索引

我如何在homecontroller中获得正确的值?

I'm new to asp.net mvc and I'm trying to create filters for my charts.I have useds several buttons for that.

@using Common
@model DateModel
<div>
    <canvas id="myChart"></canvas>
</div>
<div>
    <p>

    </p>
</div>


<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script>


    var dates = [@Html.Raw(Json.Encode(ViewBag.dates))];
    var DeletedCount = [@Html.Raw(Json.Encode(ViewBag.DeletedCount))];
    var ApprovedCount = [@Html.Raw(Json.Encode(ViewBag.ApprovedCount))];
    var ClosedCount = [@Html.Raw(Json.Encode(ViewBag.ClosedCount))];
    var OngoingCount = [@Html.Raw(Json.Encode(ViewBag.OngoingCount))];
    var NewCount = [@Html.Raw(Json.Encode(ViewBag.NewCount))];
    var ToBeAccepetdCount = [@Html.Raw(Json.Encode(ViewBag.ToBeAccepetdCount))];


    const data = {
        labels: dates[0],
        datasets: [{
            label: 'Deleted',
            backgroundColor: 'rgb(255, 99, 132)',
            borderColor: 'rgb(255, 99, 132)',
            data: DeletedCount[0],
        },
            {
                label: 'Approved',
                backgroundColor: 'rgb(0,192, 0)',
                borderColor: 'rgb(0,192, 0)',
                data: ApprovedCount[0],
            },
            {
                label: 'Closed',
                backgroundColor: 'rgb(128, 128, 128)',
                borderColor: 'rgb(128,128, 128)',
                data: ClosedCount[0],
            },
            {
                label: 'Ongoing',
                backgroundColor: 'rgb(0, 32, 255)',
                borderColor: 'rgb(0, 32, 255)',
                data: OngoingCount[0],
            },
            {
                label: 'New',
                backgroundColor: 'rgb(80, 208, 255)',
                borderColor: 'rgb(80, 208, 255)',
                data: NewCount[0],
            },
            {
                label: 'To be Accepted',
                backgroundColor: 'rgb(90, 255, 128)',
                borderColor: 'rgb(90, 255, 128)',
                data: ToBeAccepetdCount[0],
            },

        ]
    };

    const config = {
        type: 'line',
        data: data,
        options: {
            responsive: true,
            plugins: {
                legend: {
                    position: 'top',
                },
                title: {
                    display: true,
                    text: 'Tasks'
                }
            },

            scales: {
                y: {
                    beginAtZero: true
                    ,
                    title: {
                        display: true,
                        text: 'Count'
                    }
                }
            }

        },

    };
</script>
<script>
    const myChart = new Chart(
        document.getElementById('myChart'),
        config
    );
</script>

<div align="center">
    <form method="POST">
        <button type="submit" class="btn btn-light" onclick="Today()">Today</button>
        <button type="submit" class="btn btn-light">Last 7 Days</button>
        <button type="submit" class="btn btn-light">This Month</button>
        <button type="submit" class="btn btn-light">Last Month</button>
        <script>
            function Today() {
                document.getElementById('sdate').value = '2022-02-01';
                document.getElementById('edate').value = @DateTime.Now.ToString("yyyy-MM-dd");;
                console.log(document.getElementById('sdate').value);
                console.log(document.getElementById('edate').value);
            }
        </script>
    </form>
 
</div>


<hr />

<div class="row">

    <form method="POST">
        <label for="sdatelbl">Start Date</label>
        <input type="text" class="form-control" placeholder="@ViewBag.Sdate" id="sdate" name="sdate" value="" size="10" maxlength="10" />
        <label for="edatel">End Date</label>
        <input type="text" class="form-control" [email protected] id="edate" name="edate" value="" size="15" maxlength="30" />
        <br>


        <input type="submit" class="btn btn-primary" onclick="" value="Refresh">
    </form>
</div>

Today Button

When I click this I want to change sdata value in another form which is used as the cutom filter.But When I click the button it print the correct value in console,But returns null in home controller.

console output
Homecontroller Index

How can I get the correct value in homecontroller?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文