Reputation: 83
This question is partially a continuity from my previous question. I am really sorry for misusing the questioning privilege that I am granted with, but I thought I got the idea of how to navigate around MVC, but apparently not. I have the feeling that I am really close to the ending, but not sure why my code is not printing correctly.
Previous Question: How do I use post method to display a content in ASP.net core MVC
From the answer from that question, and reference to other videos found on the internet, I made a new controller, and class for the Lorem Ipsum pages:
So from the new controller, the new class and the layout page (where I get my user inputs):
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>
I am using a Lorem generator found on Github: https://github.com/dochoffiday/Lorem.NET I followed the given example on github, but am not able to print the requested lorem ipsum paragraph. What am I doing wrong?
Currently, if I click on generate, the redirections all work, but the result is:
Upvotes: 0
Views: 1177
Reputation: 2469
You have the variable defined as an array which by default you need to use a loop to access and display the data.
public string[] LorX { get; set; }
In view you need to do something like:
@for (int i = 0; i < Model.LorX.Count(); i++)
{
<p>@Model.LorX[i]</p>
}
For the non-array variables you can display them like you did: @Model.VariableName
.
Upvotes: 1