我正在使用Asp.Net MVC
构建网站。我已成功将表单数据插入数据库。但是,我的复选框出现问题,即使选中了复选框,我的数据库中的值也始终为0。
@using (Html.BeginForm())
{
<div class="modal-body">
<div class="form-group">
<label for="username" class="col-form-label">Name:</label>
<input type="text" class="form-control" **id="username" name="username" **>
</div>
<div class="row">
<div class="form-group">
<div class="col-md-6">
<input type="checkbox" **id="user_privilage" name="user_privilage" **>
<label for="user" class="col-form-label">User</label>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Add</button>
</div>
}
这是控制器中的代码:
[HttpPost]
public ActionResult UserMaintenance(User collection)
{
try
{
List<object> lst = new List<object>();
lst.Add(collection.username);
lst.Add(collection.user_privilage);
object[] allitems = lst.ToArray();
int output = db.Database.ExecuteSqlCommand("insert into DATABASE(username,user_privilage) values (@p0,@p1)", allitems);
if(output>0)
{
ViewBag.msg = "User has been added";
}
return View();
//return RedirectToAction("UserMaintenance");
}
catch {
return View();
}
}
有谁知道发生了什么?