好的,所以我刚开始使用razor mvc4,并且对c#有一点经验。我目前正在制作一个有按钮的网站。我的html如下:
<button onclick ="vote1_click" id="VoteButton" value="Vote">Vote</button>
这是在 .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)
{
}
}
}
我相信我的问题是对剃须刀结构的基本理解,但我自己无法弄清楚。
任何帮助都会受到赞赏,当答案很简单时,我会尽量不要觉得太愚蠢。
谢谢!
编辑:
我遇到了一个问题,即 Add(string name) 给我一个错误“并非所有代码路径都返回一个值”
这是我要求的其余代码:
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());
}
}
}
}
这是我的 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>
谢谢!