【发布时间】:2014-12-08 19:57:44
【问题描述】:
我有一个编辑页面,用户必须在其中选择复选框,然后将其选择的值提交到正在工作的数据库,但是有一个问题我必须捕获在创建时间。我如何在编辑页面上获得已选中的复选框值。 这是我的提交代码。
$(document).ready(function() {
$('#btn-gn').click(function () {
var list = [];
$('#MySelection input:checked').each(function () {
list.push(this.name);
});
// now names contains all of the names of checked checkboxes
// do something with it for excamle post with ajax
$.ajax({
url: '@Url.Action("RoleEdit","Role")',
type: 'POST',
data: { Parameters: list},
success: function (result) {
alert("success");
},
error: function (result) {
alert("error!");
}
}); //end ajax
});
});
<div id="MySelection">
@for (int i = 0; i < Model.PermissionsList.Count; i++)
{
<div class="row">
<div class="col-md-1 col-sm-1">
<input type="checkbox" name="tags" class="no-margin"
id="=ids" value="@Model.PermissionsList[i].PermissionId" >
</div>
<div class="col-md-11 col-sm-11">
@Model.PermissionsList[i].PermissionName
</div>
</div>
}
</div>
这是我的控制器:
[HttpGet]
public ActionResult RoleEdit(int id,ViewModelRole viewModelRole)
{
viewModelRole.Role = roleService.GetRole(id);
viewModelRole.PermissionsList = preFlightDbContext.Permissions.ToList();
return View(viewModelRole);
}
[HttpPost]
public ActionResult RoleEdit(ViewModelRole viewModelRoleForAdd, string[] tags)
{
string addPer = delemetedString(tags, ',');
try
{
if (ModelState.IsValid)
{
viewModelRoleForAdd.Role= roleService.UpdateRole(role);
roleService.SaveRole();
return RedirectToAction("RoleLists");
}
}
catch (DataException)
{
ModelState.AddModelError("", "Unable to save changes. Try again, " +
"and if the problem persists see your system administrator.");
}
return View(viewModelRoleForAdd);
}
private string delemetedString(string[] input, char delemeter)
{
string output = String.Empty;
for (int i = 0; i < input.Length; i++)
{
if (i == input.Length - 1)
output += input[i];
else
output += input[i] + delemeter;
}
return output;
}
【问题讨论】:
-
你能添加你的控制器类吗?到目前为止,代码看起来正确
-
好的,我正在更新我的代码
-
您可以在复选框上添加 checkchange 事件,并保留已更改名称的列表。
-
您想要复选框的名称吗?还是价值观?
-
@Stephen Muecke 我希望复选框出现在带有选中复选框的编辑页面中
标签: jquery asp.net-mvc checkbox