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

问题描述

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

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

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

folders of my project

因此,从新控制器、新类和布局页面(我在其中获取用户输入):

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,#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 段落。我做错了什么?

目前,如果我点击生成重定向都有效,但结果是:

Current Result of my code

解决方法

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

public string[] LorX { get; set; }

在视图中,您需要执行以下操作:

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

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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...