0

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

上一个问题:如何使用post方法在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 段落。我究竟做错了什么?

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

我的代码的当前结果

4

1 回答 1

1

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

public string[] LorX { get; set; }

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

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

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

于 2021-07-29T02:41:29.990 回答