我想使用SQL框架将单选按钮值添加到我的ASP.NET表中。我有一个包含列Approachability, Speed, Quality
的表,这三个列必须存储以下值
Satisfied, Very Satisfied, Unsatisfied
所以我使用了值的单选按钮,但我不知道如何将单选按钮值添加到表中,请任何人帮助我。
到目前为止我已经尝试过的代码:
public class Customer
{
[Required]
public string Approachability { get; set; }
[Required]
public string Speed { get; set; }
[Required]
public string Quality { get; set; }
}
控制器:
[HttpGet]
public IActionResult AddCustomer()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult AddCustomer([Bind] Customer cust)
{
if (ModelState.IsValid)
{
objproject.AddCustomer(cust);
return RedirectToAction("Index");
}
return View(cust);
}
数据库访问:
public void AddCustomer(Customer cust)
{
using (SqlConnection con = new SqlConnection(connectionstring))
{
SqlCommand cmd = new SqlCommand("spAddCustomer", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Approachability", cust.Approachability);
cmd.Parameters.AddWithValue("@Speed", cust.Speed);
cmd.Parameters.AddWithValue("@Quality", cust.Quality);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
最后,我的观点是:
<div class="col-md-10 offset-md-2">
<div class="row pt-2 row-space">
<div class="col-md-7">
<table class="table">
<thead>
<tr>
<th></th>
<th>Very Satisfied</th>
<th>Satisfied</th>
<th>Unsatisfied</th>
</tr>
</thead>
<tbody>
<tr>
<td>Approachability</td>
<td><input class="approachgroup" type="radio" asp-for="Approachability"name="approachgroup" value="Very Satisfied"></td>
<td><input class="approachgroup" type="radio" asp-for="Approachability" name="approachgroup" value="Satisfied"></td>
<td><input class="approachgroup" type="radio" asp-for="Approachability" name="approachgroup" value="UnSatisfied"></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
例如,我只包含一个字段单选按钮
发布于 2019-12-27 18:14:00
好的,对于您的特定情况,虽然有使用asp-for="SomeField"
的解决方案,但您只需将视图模型字段与单选按钮的名称字段匹配即可:
<tr>
<td>Approachability</td>
<td><input class="approachgroup" type="radio" name="Approachability" value="Very Satisfied"></td>
<td><input class="approachgroup" type="radio" name="Approachability" value="Satisfied"></td>
<td><input class="approachgroup" type="radio" name="Approachability" value="UnSatisfied"></td>
</tr>
当您提交表单时,将使用选中的单选按钮的值填充Approachability
。
https://stackoverflow.com/questions/59487357
复制相似问题