把你的數據庫邏輯放到一個控制器動作,這樣的:
我在CSHTML文件試過這種
public class HomeController : Controller
{
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(int id, FormCollection collection)
{
try
{
// Do database update logic here
// Upon successfully updating the database redirect to a view
// that displays the information, read-only version not editable
return RedirectToAction("Index");
}
catch(Exception ex)
{
// If something went wrong, then re-display the view
// the user tried to update database from
return View();
}
}
}
現在,在您的視圖通過使用HTML幫助創建表單Html.BeginForm()
,像這樣:
@using (Html.BeginForm("ActionMethodName","ControllerName"))
{
... your input, labels, textboxes and other html controls go here
<input class="button" id="submit" type="submit" value="Uložit" />
}
Note: Html.BeginForm()
will take everything inside of it and submit that as the form data to the controller action specified as parameters to it.
有一個很好的初學者教程[這裏](http://www.asp.net/web-pages/tutorials/introducing-aspnet-web-pages-2)。 – afzalulh