razor mvc4和c#codebehind

Har*_*bbs 1 html c# razor

好吧,所以我刚开始使用razor mvc4,我对c#有一点经验.我目前正在建立一个有按钮的网站.我的html如下:

<button onclick ="vote1_click"  id="VoteButton" value="Vote">Vote</button>
Run Code Online (Sandbox Code Playgroud)

这是一个.cshtml视图

然后我有一个类来处理vote1_click事件.它在c#中,如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MvcApplication1
{
    public class voting
    {
        public void vote1_click(object sender, EventArgs e)
        {
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我相信我的问题是对剃刀结构的基本理解,但我自己无法弄明白.

任何帮助都是值得赞赏的,当答案很简单时,我会尽量不觉得太愚蠢.

谢谢!

编辑:

我一直遇到一个问题,其中添加(字符串名称)给我一个错误"不是所有的代码路径都返回一个值"

这是我要求的其余代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data.SqlClient;

namespace WAgermanClub.Controllers
{
public class HomeController : Controller
{

    [HttpPost]
    public ActionResult Add(string vote)
    {




        SqlConnection vote1connection = new SqlConnection("user id=userid;" +
                                      "password=validpassword;server=o5z5dpwpzi.database.windows.net;" +
                               "Trusted_Connection=yes;" +
                               "database=wagermanclub_votes; " +
                               "connection timeout=30");
        try
        {
            vote1connection.Open();
        }
        catch (Exception g)
        {
            Console.WriteLine(g.ToString());
        }

        try
        {
            SqlDataReader myReader = null;
            SqlCommand myCommand = new SqlCommand("select * from table", vote1connection);
            myReader = myCommand.ExecuteReader();
            while (myReader.Read())
            {

                Console.WriteLine(myReader["Vote1"].ToString());
            }
        }
        catch (Exception i)
        {
            Console.WriteLine(i.ToString());
        }


        SqlCommand vote1command = new SqlCommand("INSERT INTO table (Column1, Vote1) " +
                              "Values (1, 'Vote1' + 1)", vote1connection);


        vote1command.ExecuteNonQuery();

        try
        {
            vote1connection.Close();
        }
        catch (Exception h)
        {
            Console.WriteLine(h.ToString());
        }






    }
}
Run Code Online (Sandbox Code Playgroud)

}

这是我的HTML:

@{
ViewBag.Title = "Ideas";

}

 @section featured {
<section class="featured">
    <div class="content-wrapper">
        <hgroup class="title">
            <h1>@ViewBag.Title.</h1>
            <h2>@ViewBag.Message</h2>
        </hgroup>
        <p>

        </p>
    </div>
</section>
}

<body>
<div  style="border: solid; max-width: 300px; margin-left: auto; margin-right: auto">


    @using(Html.BeginForm())
    {

    <input type="submit" value="Vote"/>
    }

 </div>




</body>
Run Code Online (Sandbox Code Playgroud)

谢谢!

Shy*_*yju 9

您对ASP.NET webforms和MVC感到困惑.MVC在经典Web(GET-POST表单)风格中运行得更多.您发布带有值的表单.代码隐藏中没有像web表单中那样的单击事件和事件处理程序.

因此,要渲染页面,您可以在HomeController中使用这样的操作方法

public class HomeController : Controller
{
   public ActionResult Add()
   {
     return View();
   }
}
Run Code Online (Sandbox Code Playgroud)

因此,在"添加"视图(razor文件)中,您需要使用一些代码来呈现带有输入元素的表单标记.让我们使用Html.Begin表单助手方法为我们呈现表单标记.

@using(Html.Beginform())
{    
  <input type="text" name="name" />
  <input type="submit" />
}
Run Code Online (Sandbox Code Playgroud)

这将在form标记中呈现标记,其中action属性设置为" Home/Add",假设您的GET操作方法位于HomeController中.(查看页面的查看源)

因此,当用户点击提交按钮时,它会将表单发布到Add操作中.所以请确保你在HomeController中有一个这样的动作方法来处理表单发布.(用HttpPost属性装饰的那个)

[HttpPost]
public ActionResult Add(string name)
{
  //do something with the posted values
  return RedirectToAction("Success");  // redirecting to another view
}
Run Code Online (Sandbox Code Playgroud)