在asp.net mvc中打印有效的字符串数组

问题描述 投票:0回答:1

这个问题部分是我之前问题的延续。我真的很抱歉滥用了我被授予的提问权限,但我以为我已经了解了如何浏览 MVC,但显然没有。我感觉我真的很接近结局了,但不确定为什么我的代码打印不正确。

上一问题:如何在ASP.net core MVC中使用post方法显示内容

根据该问题的答案,并参考互联网上的其他视频,我制作了一个新的控制器,并为 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
1个回答
1
投票

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

public string[] LorX { get; set; }

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

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

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

@Model.VariableName

© www.soinside.com 2019 - 2024. All rights reserved.