今天分享如何用Model接收Post資料,
基本上跟昨天幾乎一模一樣,
只差在接收Post的部分,
using MVCTest.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MVCTest.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
DateTime date = DateTime.Now;
ViewBag.Date = date;
Student data = new Student("1", "小明", 80);
return View(data);
}
public ActionResult Transcripts(string id, string name, int score)
{
Student data = new Student(id, name, score);
return View(data);
}
[HttpPost]
public ActionResult Transcripts(Student model)
{
string id = model.id;
string name = model.name;
int score = model.score;
Student data = new Student(id, name, score);
return View(data);
}
}
}
記得網頁的name要跟Model一樣,大小寫也要一樣,
這樣就完成了用Model接收資料,又用Model傳送資料。
這是一開始的畫面
這是開始畫面的cshtml的網頁
@{
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="post" 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>
這是接收Post並Show出資料的畫面
這是接收資料的cshtml網頁
@{
ViewBag.Title = "Transcripts";
Layout = null;
}
@model MVCTest.Models.Student
@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles/modernizr")
<div class="form-group">
<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>
完成收工!
明天終於要開始進入資料庫的部分了。
想請教一下
在您的Day12這篇裡面我嘗試增加 連接資料庫(MySql)然後使用新增insert,想要把最後的結果(@Model.id,@model.name,@model.score) insert到資料庫裡,但是一直加不進去,想知道有什麼解決的辦法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebtestPost.Models;
using MySql.Data.MySqlClient;
using System.Data;
namespace WebtestPost.Controllers
{
public class HomeController : Controller
{
string connString = "server=127.0.0.1;port=3306;user id=root;password=12345678;database=mvctestdb;charset=utf8;";
MySqlConnection conn = new MySqlConnection();
public ActionResult Index()
{
DateTime date = DateTime.Now;
ViewBag.Date = date;
Student data = new Student("12", "小明", 80);
return View(data);
}
public ActionResult Transcripts(string id, string name, int score)
{
Student data = new Student(id, name, score);
return View(data);
}
/*
[HttpPost]
public ActionResult Transcripts(FormCollection post)
{
string id = post["id"];
string name = post["name"];
int score = Convert.ToInt32(post["score"]);
Student data = new Student(id, name, score);
return View(data);
}
*/
[HttpPost]
public ActionResult Transcripts(Student model)
{
string id = model.id;
string name = model.name;
int score = model.score;
Student data = new Student(id, name, score);
conn.ConnectionString = connString;
if (conn.State != ConnectionState.Open)
conn.Open();
string sql = @"INSERT INTO 'student' ('id','name','score') VALUES (@'Model.id',@'Model.name',@'Model.score')";
MySqlCommand cmd = new MySqlCommand(sql, conn);
return View(data);
}
}
}
有錯誤訊息嗎?
沒有
等等你說Transcripts嗎?
你根本沒有執行啊...
cmd.executeNonQuery();
我打上去之後,他顯示我必須定義'@Model.id'參數,所以我是要從前端去呼叫到後端?
string sql = @"INSERT INTO 'student' ('id','name','score') VALUES (@'Model.id',@'Model.name',@'Model.score')";
這哪招...
我不知道該怎麼說了...
不過如果是@可能會被當成Parameter吧...
我覺得你還是找本C#的書先從基礎開始學...
好的 我再想一想