2013-11-23 115 views
2

你好我是cshtml的新手,我在ASP.NET中有網頁Razor v2我想在點擊按鈕時將一些數據插入到數據庫中。這些數據從各種文本框提供,並上傳圖片。請問如何提供按鈕點擊操作?ASP.NET上的按鈕點擊事件

<button type="submit" name="action" value="insertRegistered">Uložit</button> 
    @if (action == "insertRegistered") 
    { 
    var db1 = Database.Open("StarterSite"); 
    var sql = "UPDATE services SET [email protected], [email protected], [email protected] WHERE IDservice=6"; 
    db1.Execute(sql, fileName, fileContent, fileMime); 
    } 
+0

有一個很好的初學者教程[這裏](http://www.asp.net/web-pages/tutorials/introducing-aspnet-web-pages-2)。 – afzalulh

回答

3

在WebMatrix中,你可以用這種方式實現這一點:

剃刀代碼:

@{ 
    var fileName = ""; 
    var fileContent = ""; 
    var fileMime = ""; 
    var IDservice = ""; 

    @*TEST CODE *@ 
    @*if (!IsPost) 
    { 

     IDservice = "1"; 
     var db = Database.Open("StarterSite"); 
     var dbCommand = "SELECT * FROM services WHERE IDservice = @0"; 
     var row = db.QuerySingle(dbCommand, IDservice); 
     fileContent = row.fileContent; 
     fileMime = row.MimeType; 
     fileName = row.fileName; 
    } *@ 

    if (IsPost) 
    { 
     fileName = Request.Form["fileName"]; 
     fileContent = Request.Form["fileContent"]; 
     fileMime = Request.Form["fileMime"]; 
     IDservice = Request.Form["IDservice"]; 
     var db1 = Database.Open("StarterSite"); 
     var sql = "UPDATE services SET [email protected], [email protected], [email protected] WHERE [email protected]"; 
     db1.Execute(sql, fileName, fileContent, fileMime, IDservice); 
    } 
} 

而且標記應該是這樣的:

<!DOCTYPE html> 
<html> 
    <head> 
     <meta charset="utf-8" /> 
    <title>Service</title> 
     </head> 
    <body> 
    <form method="post"> 
     <fieldset> 
      <legend>Service Information</legend> 

      <p><label for="fileName">FileName:</label> 
       <input type="text" name="fileName" value="@fileName" /></p> 

      <p><label for="fileContent">File Content:</label> 
       <input type="text" name="fileContent" value="@fileContent" /></p> 

      <p><label for="fileMime">Mime:</label> 
       <input type="text" name="fileMime" value="@fileMime" /></p> 

      <input type="hidden" name="IDservice" value="@IDservice" /> 

      <p> <button type="submit" name="action" value="insert Registered">Uložit</button></p> 
     </fieldset> 
    </form> 
</body> 
</html> 

而這裏的工作sample

Here是一套教程,我相信它應該非常有幫助!

1

把你的數據庫邏輯放到一個控制器動作,這樣的:

我在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.

+0

非常感謝您的回答。我可否知道應該在課堂上使用哪個名稱空間? – Marek

+0

@Marek - 類中的命名空間,我不確定你的意思。類包含在命名空間中,所以你在問什麼類的命名空間應該放進去?您的解決方案的「控制器」目錄中應該已經有一個「Home」文件夾。 –

+0

我創建了一個名爲HomeController.cs的新類,在那裏放置了你的代碼,但Visual Studio無法識別AcceptVerbs,ActionResult,FormColeection,並且它不允許我添加一些使用名稱空間。我想我搞砸了什麼,不是嗎?我使用引擎Razor v2的ASP.NET。非常感謝您的參與。 – Marek