asp.net core 학습 노트-보기 의 데이터 전달

30819 단어 .net
보기
데이터 전송
  • ViewData:키 쌍,색인 기 를 사용 하여 데이터 에 접근 하고 임의의 형식 을 지원 합 니 다
  • ViewBag:동적 유형,ViewData의 패키지,동적 속성 으로 데 이 터 를 방문 합 니 다
  • ViewDataViewBag에 같은 이름 의 데 이 터 를 저장 하면 덮어 씁 니 다
  • using Microsoft.AspNetCore.Mvc;
    
    namespace WebApplication1.Controllers
    {
        public class TestController : Controller
        {
            //      ,"    "        
            public  IActionResult Index()
            {
                ViewData["IntTestData"] = 100;
                ViewData["StringTestData"] = "this is a string";
    
                ViewBag.IntTest2Data = 200;
                ViewBag.StringTest2Data = "this is other string";
                return View();
            }
        }
    }
    

    페이지 렌 더 링
  • Razon문법:
  • @c\#코드 블록 또는 문 구 를 나타 낸다
  • @*주석*@
  • c#코드 는html코드 와 혼합 할 수 있다
  • @{
        Layout = null;
    }
    
    
    
    <html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>Indextitle>
    head>
    <body> 
        @*        *@
        @{ 
            int i = 0;
        }
    
        @*   ViewData      *@
        @ViewData["StringTestData"]
        @*   ViewBag      *@
        @ViewBag.StringTest2Data
    body>
    html>
    
  • 복잡 한 데이터 유형의 전달
  • 컨트롤 러:
    using System.Collections.Generic;
    using Microsoft.AspNetCore.Mvc;
    
    namespace WebApplication1.Controllers
    {
        public class TestController : Controller
        {
            public  IActionResult Index()
            {
                List<string> lsData = new List<string>();
                lsData.Add("the first line");
                lsData.Add("the second line");
                lsData.Add("the third line");
                ViewBag.ListData = lsData;
                return View();
            }
        }
    }
    

    보기:
    @{
        Layout = null;
    }
    
    
    
    <html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>Indextitle>
    head>
    <body> 
        <ul>
            @foreach (var item in ViewBag.ListData)
            {
                <li>@itemli>
            }
        ul>
    body>
    html>
    
  • 양식 양식 제출
  • 전통html방식
  • 
    
    <html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>Indextitle>
    head>
    <body> 
        <form action="/test/checkuser" method="post">
            username: <input type="text" name="username" /><br />
            password: <input type="password" name="password" /><br />
                      <input type="submit" value="  " />
        form>
    body>
    html>
    
  • HtmlHelper
  • 
    
    <html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>Indextitle>
    head>
    <body> 
        <form action="/test/checkuser" method="post">
            username: @Html.TextBox("username")<br />
            password: @Html.Password("password")<br />
                      <input type="submit" value="  " />
        form>
    body>
    html>
    

    컨트롤 러:
    using Microsoft.AspNetCore.Mvc;
    
    namespace WebApplication1.Controllers
    {
        [Controller]
        public class Test : Controller
        {
            public  IActionResult Index()
            {
                return View();
            }
            [HttpPost]
            public IActionResult CheckUser(string username, string password)
            {
                return Content($"username:{username} password:{password}");
            }
        }
    }
    
  • 폼 데이터 구현
  • Controller 에 데 이 터 를 설치 합 니 다.
    using Microsoft.AspNetCore.Mvc;
    
    namespace WebApplication1.Controllers
    {
        [Controller]
        public class Test : Controller
        {
            public  IActionResult Index()
            {
                ViewBag.Username = "xiaoming";
                return View();
            }
        }
    }
    

    보기 인터페이스 에서 데이터 바 인 딩
  • html방식
  • 
    
    <html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>Indextitle>
    head>
    <body> 
        <input type="text" name="username" value="@ViewBag.Username" />
    body>
    html>
    
  • HtmlHelper방식
  • 
    
    <html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>Indextitle>
    head>
    <body> 
        @Html.TextBox("username", (string)ViewBag.Username, null, new { attr1 = "value1" })
    body>
    html>
    

    웹 페이지 소스 코드 보기attr1속성 증가:
    
    
    <html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>Indextitle>
    head>
    <body> 
        <input attr1="value1" id="username" name="username" type="text" value="xiaoming" />
    body>
    html>
    

    좋은 웹페이지 즐겨찾기