【问题标题】:When i click submit button it first inserts a default null values on first row then the next row would be the values that i inserted当我单击提交按钮时,它首先在第一行插入一个默认的空值,然后下一行将是我插入的值
【发布时间】: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


【解决方案1】:

您可能会两次点击您的AddJob 操作。首先,单击btnSave 按钮,然后通过jquery 事件,使用ajax 发布数据。然后,您的表单也将数据提交给您的AddJob Action。并插入了重复记录。

【讨论】:

    【解决方案2】:

    您两次发布表单。

    1.@(Html.BeginForm()) 发布调用,因为按钮类型是提交。 这工作正常并插入正确的数据。

    2.jQuery AJAX Post 调用。

    缺少用于获取字段值的 #。 所以,它插入了 Null 数据。

    代码:

    TaskName: $("Task_Name").val()
    

    应该是:

    TaskName: $("#Task_Name").val()
    

    如果您进行了此更改,那么它将插入数据两次。

    因此,您只需发布一次表单。

    这是可以做到的:

    1. 将按钮类型 sumit 更改为按钮。这将避免表单开始后调用。
    2. 删除 Jquery 邮政编码。

    我会建议删除 AJAX 调用代码,仅使用表单发布来发布您的表单。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-06
      • 1970-01-01
      • 2017-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-26
      相关资源
      最近更新 更多