按钮点击JSON对象有一个表单,用户应该能够取消预先选定checkbox
和节省AJAX
的职位,国家通过JSON
对象为Controller
工作在ASP.Net MVC
解析复选框值更改模型,然后在VIEW ASP.Net MVC
要求:
- 取消选中预先选定复选框抄ULD改变从“活动” =“Y”的数据库属性一排“活动=” =“N”
- 新选定的复选框会在数据库创建一个完整的新行
问题:
- 取消选定所选文字框后模型未在
VIEW
中更新。因此,服务器端数据不会更新。在数据解析为AJAX
调用之前,需要客户端更新。 - 不知道如何捕获复选框值更改并将其传递给模型,以便解析后的数据可以更改为按钮单击/提交表单。
- 下面的代码也给出了错误200,因为dataType在提交时(假设按钮在这里提交)据说不正确。
检视:
@model List<DashboardPermissionTool.ViewModels.UserViewModel>
@using (@Html.BeginForm(new { id = "UserForm" }))
{
<table class="table">
<tr>
<th>User</th>
@for (int i = 0; i < Model[0].Roles.Count; i++)
{
<th>
@Model[0].Roles[i].RoleName
</th>
}
</tr>
@for (int i = 0; i < Model.Count; i++)
{
<tr>
<td>
@Html.HiddenFor(m => m[i].UserName)
@Model[i].UserName
</td>
@for (int j = 0; j < Model[i].Roles.Count; j++)
{
<td>
@Html.CheckBoxFor(m => m[i].Roles[j].IsSelected)
</td>
}
</tr>
}
</table>
<div class="form-actions">
<a id="SubmitUserRoles" class="btn btn-success submit" value="Save">Save changes</a>
</div>
<script>
var parsedData = @Html.Raw(new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(Model));
$(document).ready(function() {
$("#SubmitUserRoles").click(function() {
$('input[type=checkbox]').on("change",function(){
if($(this).prop('checked'))
{
$(this).attr('checked', false);
}
else
{
$(this).attr('checked', true);
}
});
$.ajax({
url: '@Url.Action("Create", "User")',
type: 'POST',
contentType: 'application/json; charset=utf-8',
cache: false,
dataType: 'json',
data: JSON.stringify(parsedData),
success: function (data) {
console.log(data);
}, error: function (xhr, ajaxOptions, error) {
alert(xhr.status);
alert("Error: " + xhr.responseText);
}
});
});
});
</script>
}
HTML响应::对于每个选中复选框响应如下:
<td>
<input checked="checked" data-val="true" data-val-required="The IsSelected field is required." name="[0].Roles[0].IsSelected" type="checkbox" value="true" />
<input name="[0].Roles[0].IsSelected" type="hidden" value="false" />
</td>
对于每个选中复选框答复如下:
<td>
<input data-val="true" data-val-required="The IsSelected field is required." name="[0].Roles[1].IsSelected" type="checkbox" value="true" />
<input name="[0].Roles[1].IsSelected" type="hidden" value="false" />
</td>
的ViewModels:
public class UserViewModel
{
public string UserName { get; set; }
public List<RoleViewModel> Roles { get; set; }
}
public class RoleViewModel
{
public string RoleName { get; set; }
public bool IsSelected { get; set; }
}
控制器:
[HttpPost]
public ActionResult Create(List<UserViewModel> model)
{
for (int i = 0; i < model.Count; i++)
{
for (int j = 0; j < model[i].Roles.Count; j++)
{
db.User.Add(new User
{
username = model[i].UserName,
role = model[i].Roles[j].RoleName
});
}
db.SaveChanges();
return RedirectToAction("Index", "User");
}
return View("Index", model);
}
所有你需要的是'数据:$(“形式”)序列化()'和删除' contentType:'application/json; charset = utf-8','它将全部正确发送和绑定(我假设模型在视图中是List')。虽然它有点不清楚为什么你想在用户做出改变后仍然留在同一页面 –
并且你返回RedirectToAction(“Index”,“User”);'和'返回View(“Index”,model) ;'在方法中没有意义--ajax调用不会重定向,并且您的代码会抛出异常,因为当您实际返回'html'时指定返回类型为'json' –
您还需要删除'$('input [ ()“); on(”change“,function(){...}'脚本(不清楚你认为正在尝试做什么) –