台灣最大程式設計社群網站
線上人數
575
 
會員總數:239929
討論主題:186061
歡迎您免費加入會員
討論區列表 >> C# >> cshtml表單驗證問題
[ 變換順序 ]  
[我要回覆]
回應主題 加入我的關注話題 檢舉此篇討論 將提問者加入個人黑名單
cshtml表單驗證問題
價值 : 50 QP  點閱數:352 回應數:0

樓主

虎牙
門外漢
0 1
4 0
發送站內信


我再把html表單弄好後想結合資料庫的功能,在所有的值有填寫且依照資料庫格式的情況下是可以新增的,
但是一般使用者沒有這麼勤勞吧,所以我就改了一些些地方,但是發現2個問題,如下:

1.當表單中有值沒有輸入(這個時候沒有加入任何表單驗證),按下submit之後會出現錯誤,無法將資料新增到資料庫裡

2.在其中2個input值加入表單驗證(設為必填),其他打算不填也沒關係,就沒有用表單驗證的功能,然後按下submit之後也會出現錯誤


不好意思我是一個小小小新手,有用到資料庫的除了cshtml之外沒有學過其他的了,主要是參考了上課時候用的課本《ASP.NET 4.5 與 jQuery Mobile 跨行動裝置網頁開發》在改寫時候的一些問題,實在不太知道要如何做,還是說這些功能無法寫在一個cshtml,一定要把表單post到php或asp才能實現?

============================
(這是完整沒動過的課本範例)↓

@{
Layout="~/layouts/_Mobile.cshtml";
Page.Name = "Ch11_2_1.cshtml";
string result = "";
Validation.Add("id",
Validator.Required("你必須提供書號!"),
Validator.StringLength(5, 0, "不能超過5個字元!"));
Validation.RequireField("title", "書名不能是空的!");
Validation.RequireField("author", "作者不能是空的!");
Validation.Add("price",
Validator.Required("請輸入書價!"),
Validator.Range(100, 900, "書價必須在 100 到900之間!"));
Validation.RequireField("category", "分類不能是空的!");
Validation.Add("pubdate", Validator.DateTime("出版日必須是日期!"));
if (IsPost && Validation.IsValid()) { // 是否是表單送回
string id, title, author, price, category, pubdate;
id = Request.Form["id"];
title = Request.Form["title"];
author = Request.Form["author"];
price = Request.Form["price"];
category = Request.Form["category"];
pubdate = Request.Form["pubdate"];
var db = Database.Open("MyBooks");
string sql = "INSERT INTO Books " +
"(id, title, author, price, category, pubdate) " +
"VALUES (@0, @1, @2, @3, @4, @5)";
db.Execute(sql, id, title, author, price, category, pubdate);
result = "<p>已經成功新增一筆記錄!</p>";
}
}
@section style {
<style>
.field-validation-error { color: red; }
</style>
}
<div data-role="page">
<div data-role="header">
<h1>新增記錄</h1>
</div>
<div data-role="content">
<form method="post" action="">
<div data-role="fieldcontain">
<label for="id">書號:</label>
<input type="text" name="id" id="id"
value="@Request.Form["id"]"/>
@Html.ValidationMessage("id")
</div>
<div data-role="fieldcontain">
<label for="title">書名:</label>
<input type="text" name="title" id="title"
value="@Request.Form["title"]"/>
@Html.ValidationMessage("title")
</div>
<div data-role="fieldcontain">
<label for="author">作者:</label>
<input type="text" name="author" id="author"
value="@Request.Form["author"]"/>
@Html.ValidationMessage("author")
</div>
<div data-role="fieldcontain">
<label for="price">書價:</label>
<input type="text" name="price" id="price"
value="@Request.Form["price"]"/>
@Html.ValidationMessage("price")
</div>
<div data-role="fieldcontain">
<label for="category">分類:</label>
<input type="text" name="category" id="category"
value="@Request.Form["category"]"/>
@Html.ValidationMessage("category")
</div>
<div data-role="fieldcontain">
<label for="pubdate">出版日:</label>
<input type="text" name="pubdate" id="pubdate"
value="@Request.Form["pubdate"]"/>
@Html.ValidationMessage("pubdate")
</div>
<div data-role="fieldcontain">
<input type="submit" value="新增記錄"/>
</div>
</form>
@Html.Raw(result)
</div>
</div>

============================
(這是我改完之後跑不出來的)↓

@{
Layout="~/layouts/_Mobile.cshtml";
Page.Name = "Dwfault.cshtml";
string result = "";

Validation.RequireField("title", "書名不能是空的!");
Validation.RequireField("author", "作者不能是空的!");

if (IsPost && Validation.IsValid()) { // 是否是表單送回
string id, title, author, price, category, pubdate;

id = Request.Form["id"];
title = Request.Form["title"];
author = Request.Form["author"];
price = Request.Form["price"];
category = Request.Form["category"];
pubdate = Request.Form["pubdate"];
var db = Database.Open("MyBooks");
string sql = "INSERT INTO Books " +
"(id, title, author, price, category, pubdate) " +
"VALUES (@0, @1, @2, @3, @4, @5)";
db.Execute(sql, id, title, author, price, category, pubdate);
result = "<p>已經成功新增一筆記錄!</p>";
}
}



<div data-role="page">
<div data-role="header">
<h1>新增記錄</h1>
</div>
<div data-role="content">
<form method="post" action="" name="test">
<div data-role="fieldcontain">
<label for="id">書號:</label>
<input type="text" name="id" id="id"
value="@Request.Form["id"]"/>
</div>
<div data-role="fieldcontain">
<label for="title">書名:</label>
<input type="text" name="title" id="title"
value="@Request.Form["title"]"/>
@Html.ValidationMessage("title")
</div>
<div data-role="fieldcontain">
<label for="author">作者:</label>
<input type="text" name="author" id="author"
value="@Request.Form["author"]"/>
@Html.ValidationMessage("title")
</div>
<div data-role="fieldcontain">
<label for="price">書價:</label>
<input type="text" name="price" id="price"
value="@Request.Form["price"]"/>
</div>
<div data-role="fieldcontain">
<label for="category">分類:</label>

<input type="radio" name="category" id="category-1" value="男 @Request.Form["category"]" checked />
<label for="radio-choice-1">男</label>
<input type="radio" name="category" id="category-2" value="女 @Request.Form["category"]" />
<label for="radio-choice-2">女</label>


</div>
<div data-role="fieldcontain">
<label for="pubdate">出版日:</label>
<select name='pubdate'>
<option value="@Request.Form["pubdate"]">請選擇出版日</option>
<option value='2011/11/1 @Request.Form["pubdate"]'>2011/11/1</option>
<option value='2012/11/1 @Request.Form["pubdate"]'>2012/11/1</option>
<option value='2013/11/1 @Request.Form["pubdate"]'>2013/11/1</option>
</select>
</div>
<div data-role="fieldcontain">
<input type="submit" value="新增記錄"/>
</div>
</form>
@Html.Raw(result)
</div>
</div>

搜尋相關Tags的文章: [ cshtml ] ,
本篇文章發表於2016-12-24 16:27
別忘捐VP感謝幫助你的人 新手會員瞧一瞧
目前尚無任何回覆
[ 變換順序 ]   

回覆
如要回應,請先登入.