先在 HomeController .cs 寫傳至Transcripts的程式
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.UI.WebControls;
namespace MVCTest.Controllers
{
public class HomeController : Controller
{
public class Student
{
public string id { get; set; }
public string name { get; set; }
public int score { get; set; }
public Student()
{
id = string.Empty;
name = string.Empty;
score = 0;
}
public Student(string _id, string _name, int _score)
{
id = _id;
name = _name;
score = _score;
}
}
public ActionResult Index()
{
DateTime date = DateTime.Now;
ViewBag.Date = date;
Student data = new Student("1", "小明", 50);
return View(data);
}
public ActionResult Transcripts(string id, string name, int score)
{
Student data = new Student(id, name, score);
return View(data);
}
}
}
編輯 Home/Index.cshtml加上name 和指定路徑
@{
ViewBag.Title = "Home Page";
Layout = null;
var date = ViewBag.Date;
var student = ViewBag.Student;
var list = ViewBag.List;
}
@model MVCTest.Models.Student
@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles/modernizr")
<form style="margin-left:10px;" method="get" action="/Home/Transcripts">
<div class="form-group">
<label for="exampleInputEmail1">學號</label>
<input type="text" class="form-control" id="id" name="id" aria-describedby="emailHelp" placeholder="Enter email" value="@Model.id">
<small id="emailHelp" class="form-text text-muted">請輸入數字</small>
</div>
<div class="form-group">
<label for="exampleInputPassword1">姓名</label>
<input type="text" class="form-control" id="name" name="name" placeholder="Password" value="@Model.name">
</div>
<div class="form-group">
<label for="exampleInputEmail1">分數</label>
<input type="text" class="form-control" id="score" name="score" aria-describedby="emailHelp" placeholder="Enter email" value="@Model.score">
</div>
<button type="submit" class="btn btn-primary">確定</button>
</form>
以下是Transcripts.cshtml的部分:
1.先在views>Home的資料夾右鍵加入
2.加入內選擇檢視
3.檢視名稱輸入Transcripts>加入
@{
ViewBag.Title = "Transcripts";
//Layout = null;
}
@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles/modernizr")
<div class="form-group" method="get" action="/Home/Transcripts">
<label>學號: </label>
<label>@Model.id</label>
</div>
<div class="form-group">
<label>姓名: </label>
<label>@Model.name</label>
</div>
<div class="form-group">
<label>分數: </label>
<label>@Model.score</label>
</div>
檢討:這次在輸入時score拼錯字導致錯誤,無法及時發現
