jquery - 使用 jquery post 将表单值发布到控制器
问题描述
在一个 Asp.NET MVC 项目中,我的 cshtml 页面中有一个表单。我想使用 jquery post 适配器将表单值发送到控制器。我正在使用 .serilize() 函数来获取表单值,当我提醒序列化值时,我可以将它们作为字符串获取,但是,当我将其发送到控制器时,它会发布为 null。这是我的cshtml页面:
编辑页面.cshtml
@model Models.PROJECTS
@using (Html.BeginForm(null,null,FormMethod.Post,new {id="EditForm"}))
{
@*Html.AntiForgeryToken()*@
<div class="form-horizontal">
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.PROJE_NO)
<div class="form-group">
@Html.LabelFor(model => model.PROJECTNAME, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.PROJECTNAME, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.PROJECTNAME, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.PROJECTCODE, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.PROJECTCODE, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.PROJECTCODE, "", new { @class = "text-danger" })
</div>
</div>
.
.
.
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<i class="fa fa-save fa-lg" id="saveBtn" style="cursor:pointer;color:green" title="Edit Project"></i>
</div>
</div>
</div>
}
@section scripts
{
<script language="javascript">
$(document).ready(function() {
$(document).on("click", "#saveBtn", function () {
var formData = $('#EditForm').serialize();
alert(JSON.stringify(formData));
$.post('@Url.Action("EditProject", "Home")',
{
project: JSON.stringify(formData)
}).done(function(data) {
if (data.success) {
alertify.success(data.message);
} else {
alertify.error(data.message);
}
}).fail(function() {
alertify.error('Error while editing project');
});
});
});
</script>
这是 HomeController:
[HttpPost]
public ActionResult EditProject(PROJECTS project)
{
try
{
using (dbContext = new ProjectsDbContext())
{
dbContext.Entry(project).State = EntityState.Modified;
dbContext.SaveChanges();
return Json(new { success = true, message = "Project Info is Updated" });
}
}
catch (Exception e)
{
return Json(new { success = false, message = "Project Info Cannot be Updated" });
}
}
对于 EditProject 操作,参数 project 为 null。我不知道我在哪里做错了。任何帮助表示赞赏。
解决方案
评论后,要解决此问题,您可以使用两个更新:
1-你不需要使用JSON.stringify(formData)
,因为你使用.serialize()
的时候会生成像a这样的数据QueryString
,所以需要使用默认的contentType
: application/x-www-form-urlencoded; charset=UTF-8
,而不是as来发送JSON
。
2 -js
通过替换来改变小的功能:
{
project: JSON.stringify(formData)
}
由formData
.
js将是:
$(document).ready(function () {
$(document).on("click", "#saveBtn", function () {
var formData = $('#EditForm').serialize();
$.post('@Url.Action("EditProject", "Home")', formData)
.done(function (data) {
if (data.success) {
alertify.success(data.message);
} else {
alertify.error(data.message);
}
}).fail(function () {
//alertify.error('Error while editing project');
});
});
});
推荐阅读
- google-cloud-platform - GCP 从现有虚拟机创建实例模板
- java - 如何确定活动在哪个日历上?
- python - Python 数据表:sum、groupby、column < 0
- html - 如何为列中的图像设置中心对齐并使文本出现在其下方?
- powerbi - Power BI 增长率计算 YoY
- linux - 验证文件仅在 2 次提交之间最后被修改
- jasperserver - 7.2.0版本的jasper服务器如何查找jasper报告编译器版本
- scala - 如何优雅地从数据库初始化 Scala 服务?
- r - Knitr table 和 formattable 函数在 Rstudio & Shiny 中不起作用
- javascript - JSON:使用 Pexels API 密钥