第一步創建一個空的控制器
該控制器的代碼爲
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace WebApplication1.Controllers
{
public class Demo2Controller : Controller
{
// GET: Demo2
public ActionResult Index()
{
return View();
}
//基本類型的綁定
[HttpPost] //int? 是該int可以爲空值
public ActionResult Index(string id, int? score, string comment)
{
ViewBag.Info = $"{id}提交的評論信息是{comment},評分是{score}";
return View();
}
}
}
第三步 右鍵添加視圖
第四步 Index頁面
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<div>
@*action是提交的頁面*@
<form action="/Demo2/Index" method="post">
@*name要對應我後臺的*@
<div>賬號:<input type="text" name="id" /> </div>
<div>評分:<input type="text" name="score" /> </div>
<div>評價:<textarea name="comment" cols="50" rows="10"></textarea> </div>
<input type="submit" value="提交" />
<h1>@ViewBag.Info</h1>
</form>
</div>
</body>
</html>
效果圖
PS:
如果想提交完還保存原來的值,需要在原文本框添加value屬性指定爲ViewBag傳過來的值