2014年4月29日 星期二
[MVC] View丟字串給Controller
Author: Mango
|
at:清晨7:00
|
Category :
MVC
|
How To Pass A String With A Form Value To Controller?
View丟字串給Controller
View的部分:
命名name屬性,等等直接對應到controller的參數名稱
示範html寫法和Razor寫法
@using (Html.BeginForm())
{
<ul>
<li><input type="text" name="tbText1" /></li>
<li>CM_PKey : @Html.TextBox("tbText2", "", new { @class = "cls" })</li>
</ul>
<p><input type="submit" name="name" value="Search"/></p>
}
controller 接資料:
[HttpPost]
public ActionResult Details(string tbText1, string tbText2)
{
if(tbCMPKey.Length <= 0)
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
int id = Convert.ToInt32(tbCMPKey);
if (id == null)
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
Opera o = context.Operas.Find(id);
if (o == null)
return HttpNotFound();
return View(o);
}
參考
http://www.dreamincode.net/forums/topic/286321-how-to-pass-a-string-with-a-form-value-to-controller/
延伸其他用法
FormCollection collection
http://microsoftmasterpiece.blogspot.tw/2013/01/mvc4-razor-how-to-get-value-from-view.html
訂閱:
張貼留言 (Atom)
0 意見:
張貼留言