Program/ASP.NET
ASP.NET Core Mvc cshtml로 데이터 전달
시그너스
2020. 1. 15. 08:29
Controllers/HelloWorldController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Encodings.Web;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
namespace daorder.Controllers
{
public class HelloWorldController : Controller
{
public IActionResult Index()
{
return View();
//return "this is my default action...";
}
public IActionResult Welcome(string name, int numTimes = 1)
{
//return "This is the Welcome action method...";
//return HtmlEncoder.Default.Encode($"Hello {name}, NumTimes is: {numTimes}");
ViewData["Message"] = "Hello " + name;
ViewData["NumTimes"] = numTimes;
return View();
}
}
}
Views/Helloworld/Welcome.cshtml
@{
ViewData["Title"] = "Welcome";
}
<h2>Welcome</h2>
<ul>
@for (int i = 0; i < (int)ViewData["NumTimes"]; i++ )
{
<li>@ViewData["Message"]</li>
}
</ul>
