好吧,所以我刚开始使用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(字符串名称)给出了错误“并非所有代码路径都返回值”
这是我要求的其余代码:
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>
谢谢!
答案 0 :(得分:9)
您对ASP.NET webforms和MVC感到困惑。 MVC在经典Web(GET-POST表单)风格中运行得更多。您发布带有值的表单。代码隐藏中没有像web表单中那样的点击事件和事件处理程序。
因此,为了呈现您的页面,您可以在HomeController中使用这样的动作方法
public class HomeController : Controller
{
public ActionResult Add()
{
return View();
}
}
因此,在“添加”视图(razor文件)中,您需要使用一些代码来呈现带有输入元素的表单标记。让我们使用Html.Begin
表单帮助器方法为我们呈现表单标记。
@using(Html.Beginform())
{
<input type="text" name="name" />
<input type="submit" />
}
这将在标记中呈现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
}
答案 1 :(得分:3)
您可能会将webforms模型与asp.net mvc模型混淆。
razor仅在您使用网页或asp.net mvc时可用。
对于asp.net mvc,没有像你在这里定义的服务器方法/事件的概念。
您通常需要在控制器中定义操作方法,这些方法将负责处理您发布的任何表单。
您可以在ASP.NET MVC
上查看更多内容答案 2 :(得分:2)
MVC不实现Web表单样式viewstate和事件处理程序。所以没有vote1_click。你想要做的是
1)创建一个JavaScript Post /回到服务器
或
2)有一个表单并将所有表单变量发回服务器
这是开始MVC的一个很好的例子:http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/intro-to-aspnet-mvc-4