英文:
ASP ADO form not submitting anything and no errors showing
问题
我尝试将姓名和国家提交到数据库表,但表单没有将任何内容发送到数据库,并且我没有收到任何错误提示我哪里出错了。我漏掉了什么?谢谢。
模型
public class CustomerModel
{
/// <summary>
/// 获取或设置 CustomerId。
/// </summary>
public int CustomerId { get; set; }
/// <summary>
/// 获取或设置 Name。
/// </summary>
public string Name { get; set; }
/// <summary>
/// 获取或设置 Country。
/// </summary>
public string Country { get; set; }
}
控制器中的操作,位于 HomeController 内
[HttpGet]
public IActionResult Create()
{
return View();
}
[HttpPost]
public IActionResult Create(CustomerModel customer)
{
string constr = _config["ConnectionStrings:DefaultConnectionString"];
using (SqlConnection con = new SqlConnection(constr))
{
string query = "INSERT INTO Customers(Name, Country) VALUES(@Name, @Country)";
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Connection = con;
con.Open();
cmd.Parameters.AddWithValue("@Name", customer.Name);
cmd.Parameters.AddWithValue("@Country", customer.Country);
cmd.ExecuteNonQuery(); // 添加这一行以执行 SQL 查询
con.Close();
}
}
return View();
}
来自 Home 视图文件夹内视图的部分
@model CustomersADO.Models.CustomerModel;
<form asp-controller="Home" asp-action="Create" method="post">
<div class="mb-3">
<label asp-for="Name" class="form-label">Name</label>
<input type="text" class="form-control" asp-for="Name">
<span asp-validation-for="Name" class="text-danger"></span>
</div>
<div class="mb-3">
<label for="countryInput" class="form-label">Country</label>
<input type="text" class="form-control" id="countryInput" asp-for="Country">
<span asp-validation-for="Country" class="text-danger"></span>
</div>
<button type="submit" class="btn btn-primary" value="submit">Submit</button>
</form>
请确保在 Create
操作内添加了 cmd.ExecuteNonQuery()
以执行 SQL 查询,这样数据才会被插入到数据库中。希望这有助于解决你的问题。
英文:
I'm trying to submit a name and country to a database table but the form does not send anything to the db and I'm not getting errors to hint to what is wrong. What am I missing? Thank you.
Model
public class CustomerModel
{
///<summary>
/// Gets or sets CustomerId.
///</summary>
public int CustomerId { get; set; }
///<summary>
/// Gets or sets Name.
///</summary>
public string Name { get; set; }
///<summary>
/// Gets or sets Country.
///</summary>
public string Country { get; set; }
}
Controller action inside HomeController
[HttpGet]
public IActionResult Create()
{
return View();
}
[HttpPost]
public IActionResult Create(CustomerModel customer)
{
string constr = _config["ConnectionStrings:DefaultConnectionString"];
using (SqlConnection con = new SqlConnection(constr))
{
string query = "INSERT INTO Customers(Name, Country) VALUES(@Name, @Country)";
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Connection = con;
con.Open();
cmd.Parameters.AddWithValue("@Name", customer.Name);
cmd.Parameters.AddWithValue("@Country", customer.Country);
con.Close();
}
}
return View();
}
From view inside Home view folder
@model CustomersADO.Models.CustomerModel;
<form asp-controller="Home" asp-action="Create" method="post">
<div class="mb-3">
<label asp-for="Name" class="form-label">Name</label>
<input type="text" class="form-control" asp-for="Name">
<span asp-validation-for="Name" class="text-danger"></span>
</div>
<div class="mb-3">
<label for="countryInput" class="form-label">Country</label>
<input type="text" class="form-control" id="countryInput" asp-for="Country">
<span asp-validation-for="Country" class="text-danger"></span>
</div>
<button type="submit" class="btn btn-primary" value="submit">Submit</button>
</form>
答案1
得分: 0
你需要调用SqlCommand的Execute方法之一。我会使用ExecuteNonQuery,因为它将返回受影响的行数。
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Connection = con;
con.Open();
cmd.Parameters.AddWithValue("@Name", customer.Name);
cmd.Parameters.AddWithValue("@Country", customer.Country);
cmd.ExecuteNonQuery();
con.Close();
}
英文:
You need to call one of the Execute methods against the SqlCommand. I would use ExecuteNonQuery as it will return the number of row affected.
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Connection = con;
con.Open();
cmd.Parameters.AddWithValue("@Name", customer.Name);
cmd.Parameters.AddWithValue("@Country", customer.Country);
cmd.ExecuteNonQuery()
con.Close();
}
Here is the documentation for that method.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论