我想使用$ .POST或$ .AJAX实现创建(INSERT)屏幕。
注意:代码工作正常,没有AJAX调用..已经存在..现在我需要做ajax调用并保存而不回发。 我在下面写了代码:
在提交点击事件后面是代码:
$.ajax(
{
url: '@Url.Action("CreateProduct","ProductManagement")',
dataType: 'json',
contentType: 'application/json; charset=utf-8',
type: 'post',
data: $('frm').serialize(),
success: function () { alert('s'); },
error: function (e1, e2, e3) { alert(e2); alert(e1 + ' ' + e2 + ' ' + e3); alert('failure event'); }
}
);
});
在服务器端:
[HttpPost]
public JsonResult CreateProduct(FormCollection frm)
{
manager.ProductManager m = new manager.ProductManager();
// m.InsertProduct(new common.DTO.ProductDTO() { Id = id, ProductName = ProductName, Description = Description, Cost = cost, ProductTypeId = 5 });
return Json("'result':'success'", JsonRequestBehavior.AllowGet);
}
问题是,每次调用操作但在frm
参数参数上找不到数据。
我也试图保持 - 查看模型ProductViewModel vm
作为参数,但它不起作用..只给我空值。而且,在ajax电话中,它取得了成功但是。只是问题在控制器的行动中没有发布。
Html如下:
@using (Html.BeginForm("CreateProduct", "ProductManagement", FormMethod.Post, new { id = "frm" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>ProductViewModel</legend>
<div id="CreateDiv">
<div class="editor-label">
@Html.LabelFor(model => model.ProductName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ProductName)
@Html.ValidationMessageFor(model => model.ProductName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Cost)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Cost)
@Html.ValidationMessageFor(model => model.Cost)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Description)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Description)
@Html.ValidationMessageFor(model => model.Description)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.ProductTypeId)
</div>
<div class="editor-field">
@Html.DropDownList("ProductTypeId", "Choose item")
@Html.ValidationMessageFor(model => model.ProductTypeId)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.ProductTypeName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ProductTypeName)
@Html.ValidationMessageFor(model => model.ProductTypeName)
</div>
</div>
<p>
<input type="submit" value="Create" id="btnSubmit" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
请指导我这里有什么问题。
谢谢
答案 0 :(得分:1)
对于id Selector,您必须使用&#34;#&#34;与id。
这将有效:
$.ajax(
{
url: '@Url.Action("CreateProduct","ProductManagement")',
dataType: 'json',
contentType: 'application/json; charset=utf-8',
type: 'post',
data: $('this').serialize(), OR $('#frm').serialize(), <-----------------
success: function () { alert('s'); },
error: function (e1, e2, e3) { alert(e2); alert(e1 + ' ' + e2 + ' ' + e3); alert('failure event'); }
}
);
或试试这个:
var form = $('#frm');
$.ajax({
cache: false,
async: true,
dataType: 'json',
contentType: 'application/json; charset=utf-8',
type: "POST",
url: form.attr('action'),
data: form.serialize(),
success: function (data) {
alert(data);
}
});
答案 1 :(得分:0)
您忘记添加ID选择器:
$.ajax(
{
url: '@Url.Action("CreateProduct","ProductManagement")',
dataType: 'json',
contentType: 'application/json; charset=utf-8',
type: 'post',
data: $('#frm').serialize(), // $('#frm');
success: function () { alert('s'); },
error: function (e1, e2, e3) { alert(e2); alert(e1 + ' ' + e2 + ' ' + e3); alert('failure event'); }
}
);
答案 2 :(得分:0)
如果表单的ID为frm
,则应引用如下
data: $("#frm").serialize(),