c# - 如何获得多个值?
问题描述
我正在学习 ASP.NET MVC,我正在尝试获取多个选中的复选框值。但是当我选择多个值时,我只会得到一个值。请帮助我:如何获得所有选定的值?
这是我的课堂方法:
public void AddEmployee(Employee emp)
{
using (SqlConnection con = new SqlConnection(ConnectionString))
{
SqlCommand cmd = new SqlCommand("spaddEmployee", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@FirstName", emp.FirstName);
cmd.Parameters.AddWithValue("@LastName", emp.LastName);
cmd.Parameters.AddWithValue("@Gender", emp.Gender);
cmd.Parameters.AddWithValue("@DOB", Convert.ToDateTime(emp.DOB));
cmd.Parameters.AddWithValue("@Hobby",string.Join(",",emp.Hobby));
cmd.Parameters.AddWithValue("@Photo", emp.Photo);
cmd.Parameters.AddWithValue("@City", emp.City);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
public IEnumerable<clsHobbyList> GetHobby()
{
List<clsHobbyList> lstHobby = new List <clsHobbyList>();
using (SqlConnection con = new SqlConnection(ConnectionString))
{
SqlCommand cmd = new SqlCommand("spAddHoby", con);
cmd.CommandType = CommandType.StoredProcedure;
con.Open();
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
clsHobbyList hby = new clsHobbyList();
hby.Id = Convert.ToInt32(rdr["Id"]);
hby.Hobby = rdr["Hobby"].ToString();
lstHobby.Add(hby);
}
con.Close();
}
return lstHobby;
}
这是我的控制器:
[HttpGet]
public ActionResult Create()
{
EmployeeDataAccessLayer objemployee = new EmployeeDataAccessLayer();
Employee emp = new Employee();
ViewBag.Hobby = new SelectList(objemployee.GetHobby(), "Id", "Hobby");
return View(emp);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind] Employee emp, HttpPostedFileBase file)
{
EmployeeDataAccessLayer objemployee = new EmployeeDataAccessLayer();
objemployee.GetHobby();
if (file != null)
{
string pic = Path.GetFileName(file.FileName);
string path = Path.Combine(Server.MapPath("~/Upload/"), pic);
// file is uploaded
file.SaveAs(path);
emp.Photo = "/Upload/" + pic;
}
if (ModelState.IsValid)
{
ViewBag.Hobby = new SelectList(objemployee.GetHobby(), "Id", "Hobby");
objemployee.AddEmployee(emp);
return RedirectToAction("Index");
}
return View(emp);
}
这是我的看法:
<div class="form-group">
@Html.LabelFor(model => model.Hobby, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@foreach (var item in ViewBag.Hobby)
{
<input type="checkbox" id="Hobby" name="Hobby" value="@item.Text" />
@item.Text
}
</div>
@Html.ValidationMessageFor(model => model.Hobby, "", new { @class = "text-danger" })
</div>
请提出一些解决方案 - 我怎样才能获得所有选定的复选框值?
解决方案
您需要使用MultiSelectList
,如下所示:
员工模型
public class Employee
{
public Employee()
{
SelectedHobbies = new List<int>();
...
}
[Display(Name = "Please Select Hobbies")]
public List<int> SelectedHobbies { get; set; }
public MultiSelectList AvailableHobbies { get; set; }
...
}
控制器
[HttpGet]
public ActionResult Create()
{
EmployeeDataAccessLayer objemployee = new EmployeeDataAccessLayer();
Employee emp = new Employee()
{
AvailableHobbies = new MultiSelectList(objemployee.GetHobby(), "Id", "Hobby")
};
return View(emp);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Employee emp)
{
var selectedHobbies = emp.SelectedHobbies;
...
return RedirectToAction("Index");
}
看法
@using (Html.BeginForm("Create", "Home", FormMethod.Post))
{
@Html.AntiForgeryToken()
...
<div class="form-group row">
@Html.LabelFor(model => model.SelectedHobbies, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@foreach (var item in Model.AvailableHobbies)
{
<div class="checkbox">
<label>
<input type="checkbox"
name="SelectedHobbies"
value="@item.Value"
@if (Model.SelectedHobbies.Contains(Int32.Parse(item.Value))) { <text> checked </text> } /> @item.Text
</label>
</div>
}
</div>
</div>
...
<div class="row">
<input type="submit" value="Create" />
</div>
}
推荐阅读
- azure-devops - 使用 Azure Build API 将参数传递给 azure yaml 管道时遇到问题
- azure - Azure DevOps PR Build Validation 仅检查其中一项策略
- r - R中多列标志的阈值
- java - 代表崩溃游戏的指数分布
- python - 在 HBox 容器中将两个 ipywidgets textAreas 扩展到最大
- angular - 自定义元素(聚合物)属性绑定到 Angular 9 中的 RxJS Observable
- c - 是否可以仅在缓冲区末尾存储 n-1 个字符?
- c# - 我可以强制 Json.NET 从根目录开始树反序列化吗
- excel - VBA 编码变量属性的类型不正确
- css - 如何将 css 文件添加到 R Shiny 以改进我的应用程序?