【发布时间】:2020-06-18 14:26:55
【问题描述】:
我是 MVC asp.net jquery ajax 的新手,希望你们能帮助我深入了解我的问题。 非常感谢您的任何回答都会有所帮助。
我刚刚在某个在线小组上看到了一个自我练习,以帮助我获得有关如何在 asp.net mvc 上实现 jquery 的更多知识,任何建议都会有所帮助。
Controller
public JsonResult AddJob(jobdetail job)
{
using(jQueryAjaxEntities db = new jQueryAjaxEntities())
{
db.jobdetails.Add(job);
db.SaveChanges();
return Json("Success");
}
}
这是我的 AddJob 视图 每当我删除它正常保存的脚本 src 时,谁能解释我为什么会这样?
<script src="~/Scripts/jquery-3.4.1.min.js"></script>
<script>
$(document).ready(function () {
$("#btnSave").click(function () {
var JobModel = {
TaskName: $("Task_Name").val(),
Description: $("Description").val(),
DateStarted: $("Date_Started").val(),
DateFinished: $("Date_Finished").val(),
Status: $("Status").val()
};
$.ajax({
type: "POST",
url: "/Home/AddJob",
dataType: "json",
contentType: "application/json",
data: JSON.stringify({ job: JobModel }),
success: function (response) {
window.location.href = "/Home/Index"
}
});
});
});
</script>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.JobID)
<div class="form-group">
@Html.LabelFor(model => model.Task_Name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Task_Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Task_Name, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Description, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Date_Started, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Date_Started, new { htmlAttributes = new { @class = "form-control", @type = "date" } })
@Html.ValidationMessageFor(model => model.Date_Started, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Date_Finished, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Date_Finished, new { htmlAttributes = new { @class = "form-control", @type = "date" } })
@Html.ValidationMessageFor(model => model.Date_Finished, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Status, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Status, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Status, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group" id="DivSave">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Submit" id="btnSave" class="btn btn-default" />
</div>
</div>
</div>
}
【问题讨论】:
-
您忘记为所有 jquery 元素(如
$("Task_Name").val()等)添加#。 id 需要选择器,例如$("#Task_Name") -
@palaѕн 谢谢你的建议,但它仍在发生,先生,但它现在重复行先生
-
然后尝试使用名称属性为所有类似
$("[name='Task_Name'").val()或添加特定的ID 到EditorFor -
@palaѕн 尝试了这两个仍然重复:(
标签: c# jquery asp.net ajax asp.net-mvc