【发布时间】:2014-08-03 13:22:51
【问题描述】:
我想使用 $.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>
请指导我这里出了什么问题。
谢谢
【问题讨论】:
-
在服务器端代码中您忘记提供名为的属性 - [HTTPPost] 没有 CreateProduct() 方法。
-
它的 der.. 忘记了 post.apologies。更新了上面的代码。
-
@user3711357...现在获取表单值???
标签: jquery ajax asp.net-mvc-4 asp.net-ajax