본문 바로가기

Program/ASP.NET

ASP.NET Core Mvc cshtml로 데이터 전달

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>

https://localhost:44331/HelloWorld/Welcome?name=Rick&numTimes=4

'Program > ASP.NET' 카테고리의 다른 글

ASP.NET Core Razor page 권한 부여  (0) 2020.01.14
asp.net core razor 패키지관리자 콘솔 명령어  (0) 2020.01.09
data class 지정방법  (0) 2020.01.09