我正在研究MVC3并尝试使用JQuery和JSON将值表单视图传递给控制器。该查询正在提取网格内的复选框的值。以下是代码
<script type="text/javascript">
function DeleteCustomer() {
if (confirm("Are you sure to delete records")) {
$('#myGrid table tr').each(function () {
if ($(this).find("input[id*='assignChkBx']").length > 0) {
if ($(this).find("input[id*='assignChkBx']")[0].checked == true) {
//var CustID = $(this).find("input[id*='assignChkBx']").attr("CustomerID");
var id = $(this).find("input[id*='assignChkBx']").val();
var DTO = JSON.stringify(id);
alert(DTO); **//Showing right value**
var temp = $(this);
$.ajax({
url: "Customer/DeleteCustomeByID",
type: "POST",
dataType: "json",
data: DTO,
contentType: 'application/json',
success: function (data) {
alert('Success');
},
error: function (xhr, textStatus, err) {
alert('Error :(' + err);
}
});
}
}
});
}
}
'DTO'以上是复选框值,它给出正确的值。
以下是我的HTML
<input type="button" id="btnDelete" value="Delete" title="Delete" onclick="DeleteCustomer()" style="color:steelblue"/>
<div id="myGrid" style="width:100%">
@*@Html.Partial("_grid", Model)*@
@{
WebGrid grid = new WebGrid(Model, rowsPerPage: 15, ajaxUpdateContainerId: "myGrid");
}
@grid.GetHtml(
//htmlAttributes: new { id = "myGrid1" },
fillEmptyRows: false,
alternatingRowStyle: "alternate-row",
headerStyle: "grid-header",
footerStyle: "grid-footer",
mode: WebGridPagerModes.All,
firstText: "<< First",
previousText: "< Prev",
nextText: "Next >",
lastText: "Last >>",
columns: new[] {
grid.Column(header: "", format: @<text><input class="check-box" type="checkbox" id="assignChkBx" value="@item.CustomerID" /></text>),
//grid.Column(header: "", format: @<text>@Html.CheckBox("assignChkBx") </text>),
grid.Column("CustomerID", "CustomerID", canSort: true, style: "CustomerID"),
grid.Column("CompanyName", "Company Name", canSort: true, style: "CompanyName"),
grid.Column("ContactName", "Contact Name", canSort: true, style: "ContactName"),
grid.Column("Address", "Address", canSort: false, style: "Address"),
grid.Column("City", "City", canSort: true, style: "City"),
grid.Column("",
header: "Actions",
format: @<text>
@Html.ActionLink("Edit", "Edit", new { id=item.CustomerID} )
@Html.ActionLink("Delete", "Delete", new { id=item.CustomerID} )
</text>
)
})
将值从视图传递给控制器时出现问题。请查看以下代码:
//Delete multiple Customers by CustomeByID
[HttpPost]
public ActionResult DeleteCustomeByID(string DTO)
{
try
{
// delete the record from ID
Customer customer = db.Customers.Find(DTO);
db.Customers.Remove(customer);
db.SaveChanges();
return RedirectToAction("Index");
//return View(customer);
}
catch (DbEntityValidationException dbEx)
{
foreach (var validationErrors in dbEx.EntityValidationErrors)
{
foreach (var validationError in validationErrors.ValidationErrors)
{
Trace.TraceInformation("Class: {0}, Property: {1}, Error: {2}",
validationErrors.Entry.Entity.GetType().FullName,
validationError.PropertyName,
validationError.ErrorMessage);
}
}
throw new Exception();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
这里发生了两个错误。
我。变量DTO正在发出'null'
II。当' db.Customers.Remove(customer); '语句执行时,我收到以下错误。
值不能为空。 参数名称:实体
我无法理解为什么会发生这些错误。 删除语句在没有JQuery的情况下传递参数时工作正常。
我正在使用Northwing数据库。
由于
答案 0 :(得分:0)
<script type="text/javascript">
function DeleteCustomer() {
if (confirm("Are you sure to delete records")) {
$('#myGrid table tr').each(function () {
if ($(this).find("input[id*='assignChkBx']").length > 0) {
if ($(this).find("input[id*='assignChkBx']")[0].checked == true) {
//var CustID = $(this).find("input[id*='assignChkBx']").attr("CustomerID");
var id = $(this).find("input[id*='assignChkBx']").val();
id="{'DTO':'+ id +'}" **//This is change**
var DTO = JSON.stringify(id);
alert(DTO); **//Showing right value**
var temp = $(this);
$.ajax({
url: "Customer/DeleteCustomeByID",
type: "POST",
dataType: "json",
data: DTO,
contentType: 'application/json',
success: function (data) {
alert('Success');
},
error: function (xhr, textStatus, err) {
alert('Error :(' + err);
}
});
}
}
});
}
}
只需检查我的代码
id =“{'DTO':'+ id +'}” //这是更改