【问题标题】:Printing valid string array in asp.net mvc在 asp.net mvc 中打印有效的字符串数组
【发布时间】:2021-10-04 17:22:14
【问题描述】:

这个问题部分是我上一个问题的连续性。我真的很抱歉滥用了我被授予的提问特权,但我认为我知道如何在 MVC 中导航,但显然没有。我感觉我真的很接近结局了,但不确定为什么我的代码没有正确打印。

上一个问题:How do I use post method to display a content in ASP.net core MVC

根据该问题的答案,并参考互联网上的其他视频,我为 Lorem Ipsum 页面制作了一个新控制器和类:

所以从新控制器、新类和布局页面(我获取用户输入的地方):

LOSx.cshtml:

@{
    ViewData["Title"] = "Lorem Ipsum Generator";
    Layout = "/Views/Shared/_Layout.cshtml";
}



<style>
    body {
        /* Margin bottom by footer height */
        margin-bottom: 60px;
        font-family: Kalam, cursive;
    }

    nav {
        height: 100%;
        width: 100%;
        left: 0;
        right: 0;
        top: 0;
        bottom: 0;
        position: absolute;
        background: linear-gradient(135deg, #a8e6cf, #dcedc1, #ffd3b6, #ffaaa5, #ff8b94);
        background-size: 200% 200%;
        animation: rainbow 10s alternate infinite;
    }

    .JK {
        padding: 15px 25px;
        font-size: 24px;
        text-align: center;
        cursor: pointer;
        outline: none;
        color: #fff;
        background-color: #04AA6D;
        border: none;
        border-radius: 15px;
        box-shadow: 0 9px #999;
    }

        .JK:hover {
            background-color: #3e8e41
        }

        .JK:active {
            background-color: #3e8e41;
            box-shadow: 0 5px #666;
            transform: translateY(4px);
        }
</style>

<html>
<body class="text-center">
    <form action="GetLos" method="post">
        <h1 style="font-size:26px;">Lorem Ipsum Generator</h1>
        <p>At this page, You will get to generate Lorem Ipsum text, at your desired size and format. Enjoy!</p>
        <p>Enter Number of words to generate: <input name="wordx" type="text" value="10" /></p>
        <p>Enter Number of sentences to generate: <input name="senX" type="text" value="3" /></p>
        <p>Enter Number of paragraphs to generate: <input name="parax" type="text" value="2" /></p>
        <p>Include Data and time at the end of the list? (Proxy is allowed!)  <input type="radio" name="DtY" />Yes    <input type="radio" name="DtN" checked="checked" />No</p>
        <p>If you have selected the option "Yes", Please choose a specific time and date: <input type="datetime-local" name="datetimex" /></p>
        <p>Do you want the generator to start with "Lorem ipsum dolor sit amet" (Check the box, only if you want): <input type="checkbox" id="startx" />Yes</p>
        <p><input type="submit" class="JK" value="Generate!" /></p>
        <p></p>
    </form>
</body>
</html>

LotIpsController.cs:

using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Project_Divinity.Models;

namespace Project_Divinity.Controllers
{
    public class LorIpsController : Controller
    {
        public IActionResult LOSx()
        {
            return View();
        }

        public ActionResult GetLos(int wordx, int parax, bool DtY, bool DtN, string datetimex, bool startx, int senX, string[] LorX)
        {
            LorIps Fx = new LorIps
            {
                senX = senX,
                wordx = wordx,
                DtN = DtN,
                DtY = DtY,
                datetimex = datetimex,
                parax = parax,
                startx = startx,
                LorX = LoremNET.Lorem.Paragraphs(wordx, senX, parax).ToArray()
        };

            return View(Fx);
        }
    }
}

LorIps.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Project_Divinity.Models
{
    public class LorIps
    {
        public int senX { get; set; }
        public int wordx { get; set; }
        public int parax { get; set; }
        public bool DtY { get; set; }
        public bool DtN { get; set; }
        public string datetimex { get; set; }
        public bool startx { get; set; }
        public string[] LorX { get; set; }
    }
}

GetLos.cshtml:

@model Project_Divinity.Models.LorIps;

    <style>
        body {
            /* Margin bottom by footer height */
            margin-bottom: 60px;
            font-family: Kalam, cursive;
            text-align:center;
        }

        nav {
            height: 100%;
            width: 100%;
            left: 0;
            right: 0;
            top: 0;
            bottom: 0;
            position: absolute;
            background: linear-gradient(135deg, #a8e6cf, #dcedc1, #ffd3b6, #ffaaa5, #ff8b94);
            background-size: 200% 200%;
            animation: rainbow 10s alternate infinite;
        }
    </style>

<html>
<body>
    <h1>Hi</h1>
    <div>
        <p>@Model.LorX</p>
    </div>
</body>
</html>

我正在使用 Github 上的 Lorem 生成器:https://github.com/dochoffiday/Lorem.NET 我遵循了 github 上的给定示例,但无法打印请求的 lorem ipsum 段落。我做错了什么?

目前,如果我点击生成,重定向所有工作,但结果是:

【问题讨论】:

    标签: c# arrays asp.net-mvc model lorem-ipsum


    【解决方案1】:

    您将变量定义为一个数组,默认情况下您需要使用循环来访问和显示数据。

    public string[] LorX { get; set; }

    鉴于您需要执行以下操作:

    @for (int i = 0; i < Model.LorX.Count(); i++)
    {
        <p>@Model.LorX[i]</p>
    }
    

    对于非数组变量,您可以像以前一样显示它们:@Model.VariableName

    【讨论】:

    • 所以在我的 GetLos.cshtml 中,我必须添加一个新的 &lt;script&gt; 部分并添加上述功能?我在有和没有&lt;script&gt; 部分的情况下都试过了,它会导致错误,也就是说在这个上下文中找不到'i'。
    • 您需要将当前的&lt;p&gt;@Model.LorX&lt;/p&gt; 替换为for 循环。
    • 该死的,它奏效了。 :) 现在我只需要处理程序的其余部分。耶
    猜你喜欢
    • 2020-03-13
    • 1970-01-01
    • 2019-04-23
    • 2021-09-21
    • 2021-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多