ASP ADO 表单未提交任何内容,也没有显示任何错误。

huangapple go评论58阅读模式
英文:

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
{
    ///&lt;summary&gt;
    /// Gets or sets CustomerId.
    ///&lt;/summary&gt;
    public int CustomerId { get; set; }

    ///&lt;summary&gt;
    /// Gets or sets Name.
    ///&lt;/summary&gt;
    public string Name { get; set; }

    ///&lt;summary&gt;
    /// Gets or sets Country.
    ///&lt;/summary&gt;
    public string Country { get; set; }
}

Controller action inside HomeController

[HttpGet]
public IActionResult Create()
{
    return View();
}

[HttpPost]
public IActionResult Create(CustomerModel customer)
{
    string constr = _config[&quot;ConnectionStrings:DefaultConnectionString&quot;];
    using (SqlConnection con = new SqlConnection(constr))
    {
        string query = &quot;INSERT INTO Customers(Name, Country) VALUES(@Name, @Country)&quot;;

        using (SqlCommand cmd = new SqlCommand(query))
        {
            cmd.Connection = con;
            con.Open();
            cmd.Parameters.AddWithValue(&quot;@Name&quot;, customer.Name);
            cmd.Parameters.AddWithValue(&quot;@Country&quot;, customer.Country);
            
            con.Close();
        }
    }

    return View();
}

From view inside Home view folder

@model CustomersADO.Models.CustomerModel;


&lt;form asp-controller=&quot;Home&quot; asp-action=&quot;Create&quot; method=&quot;post&quot;&gt;
    &lt;div class=&quot;mb-3&quot;&gt;
        &lt;label asp-for=&quot;Name&quot; class=&quot;form-label&quot;&gt;Name&lt;/label&gt;
        &lt;input type=&quot;text&quot; class=&quot;form-control&quot; asp-for=&quot;Name&quot;&gt;
        &lt;span asp-validation-for=&quot;Name&quot; class=&quot;text-danger&quot;&gt;&lt;/span&gt;
    &lt;/div&gt;
    &lt;div class=&quot;mb-3&quot;&gt;
        &lt;label for=&quot;countryInput&quot; class=&quot;form-label&quot;&gt;Country&lt;/label&gt;
        &lt;input type=&quot;text&quot; class=&quot;form-control&quot; id=&quot;countryInput&quot; asp-for=&quot;Country&quot;&gt;
        &lt;span asp-validation-for=&quot;Country&quot; class=&quot;text-danger&quot;&gt;&lt;/span&gt;
    &lt;/div&gt;

    &lt;button type=&quot;submit&quot; class=&quot;btn btn-primary&quot; value=&quot;submit&quot;&gt;Submit&lt;/button&gt;
&lt;/form&gt;

答案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(&quot;@Name&quot;, customer.Name);
        cmd.Parameters.AddWithValue(&quot;@Country&quot;, customer.Country);
        cmd.ExecuteNonQuery()
        con.Close();
    }

Here is the documentation for that method.

huangapple
  • 本文由 发表于 2023年7月10日 22:39:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76654841.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定