そのため、クリックするとデータがSQLサーバーに送信されるボタンを作成しようとしています。送信ボタンのみを持つフォームを作成するだけで、ホームコントローラーでその送信ボタンを処理できると思いました。
私のhtmlは次のとおりです。
<body>
<div style="border: solid; max-width: 300px; margin-left: auto; margin-right: auto">
@using(Html.BeginForm())
{
<input type="submit" value="Vote"/>
}
</div>
</body>
私のホームコントローラーでは、次のようなフォームを処理することになっていると思いました:
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 vote1)
{
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());
}
}
}
}
add(string vote1) ですべてのコードパスが値を返すわけではないというエラーが表示されることを除いて、すべて問題ないように見えます。私は何を間違っていますか?私はHTMLフォームをそれほど把握していませんが、これを取得すると、理解が深まると感じています。
すべての助けに感謝します!ありがとう!