asp.net core 학습 노트-보기 의 데이터 전달
30819 단어 .net
데이터 전송
ViewData
:키 쌍,색인 기 를 사용 하여 데이터 에 접근 하고 임의의 형식 을 지원 합 니 다ViewBag
:동적 유형,ViewData
의 패키지,동적 속성 으로 데 이 터 를 방문 합 니 다ViewData
와ViewBag
에 같은 이름 의 데 이 터 를 저장 하면 덮어 씁 니 다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}");
}
}
}
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>
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
.net 데이터베이스 조작 프레임워크 SqlSugar의 간단한 시작SqlSugar는 오래된 브랜드입니다.NET 데이터베이스 조작 프레임워크, 과당 빅데이터 과학기술팀이 유지 보수 및 업데이트, Github star 수 EF 및 Dapper 버금가 지원 데이터베이스: MySql, S...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.