【问题标题】:ASP.NET on button click eventASP.NET 上的按钮单击事件
【发布时间】:2013-11-24 05:08:14
【问题描述】:

您好,我是 cshtml 新手,我在 ASP.NET Razor v2 中有网页,我想在单击按钮时将一些数据插入 DB。这些数据来自各种文本框,也来自上传图片。请问如何在按钮单击时提供操作?

我在我的 cshtml 文件中试过这个:

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

【问题讨论】:

  • 有一个不错的新手教程here

标签: asp.net webmatrix asp.net-webpages


【解决方案1】:

在 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 FileName=@0, FileContent=@1, MimeType=@2 WHERE IDservice=@3";
        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 是一组教程,我相信应该很有帮助!

【讨论】:

    【解决方案2】:

    将您的数据库逻辑放入控制器操作中,如下所示:

    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" />
    }
    

    注意:Html.BeginForm() 将获取其中的所有内容并将其作为表单数据提交给指定为参数的控制器操作。

    【讨论】:

    • 非常感谢您的回答。我可以知道我应该在课堂上使用哪个命名空间吗?
    • @Marek - 类中的命名空间,我不确定你的意思。类包含在命名空间中,所以您是在问该类应该放在哪个命名空间中?您的解决方案的 Controllers 目录中应该已经有一个 Home 文件夹。
    • 我创建了一个名为 HomeController.cs 的新类,将您的代码放在那里,但 Visual Studio 无法识别 AcceptVerbsActionResultFormColeection,并且它没有让我添加一些 using namespace。我想我搞砸了什么,不是吗?我正在使用带有引擎 Razor v2 的 ASP.NET。非常感谢您的宝贵时间。
    • 您是否使用 Visual Studio 模板创建了 ASP.NET MVC 应用程序?
    • @Marek 你已经创建了一个 Razor 网页站点。 Karl Anderson 的代码用于 MVC。你不能把两者混在一起。删除 System.Web.Mvc.dll 并忽略与控制器有关的所有内容。你应该看看 afzalulh 的回答。
    猜你喜欢
    • 2013-05-14
    • 2014-01-14
    • 1970-01-01
    • 2010-10-21
    • 2011-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多