简体   繁体   English

保存部分视图后如何保持在同一页面上

[英]How to stay on same page after saving partial view

I have an ajax request for a partial view working fine and gets the information i need for the user to edit, now when the user presses the save button the data saves but it it keeps redirecting to CameraInfo/Index which doesnt exist, is it possible to save and stay on the same page? 我有一个ajax请求,可以正常工作的部分视图,并获取我需要用户编辑的信息,现在当用户按下保存按钮时,数据将保存,但它会一直重定向到不存在的CameraInfo / Index保存并保留在同一页面上? or even return to the home page if it isnt possible? 甚至无法返回首页? below is the method involded in the HTTP Post and get code involved 以下是HTTP Post中涉及的方法并涉及代码

AJAX AJAX

 $(document).ready(function () {
        $('#qr-number').on('change', function () {
            $.ajax({
                type: "Get",
                url: '/CameraInfo/Edit',
                data: { id: $('#qr-number').val() },
                success: function (response) {
                    $('#Sample').html(response);
                },
                error: function (response) {
                    if (response.responseText != "") {
                        alert(response.responseText);
                        alert("Some thing wrong..");
                    }
                }
            });
        });
    });

_CameraInfo.cshtml (partial view) _CameraInfo.cshtml(部分视图)

@model JobTracker.Models.Job


<h2>Edit and Confirm</h2>

@using (Html.BeginForm()) {
@Html.ValidationSummary(true)

<fieldset>
    <legend>Job</legend>

    @Html.HiddenFor(model => model.JobID)

    @Html.HiddenFor(model => model.OrderID)

  <div class="editor-label">
        @Html.LabelFor(model => model.LocationID, "Location")
    </div>
    <div class="editor-field">
        @Html.DropDownList("LocationID", String.Empty)
        @Html.ValidationMessageFor(model => model.LocationID)
    </div><br />

    <div class="editor-label">
        @Html.LabelFor(model => model.HighPriority)
    </div>
    <div class="editor-field">
        @Html.DropDownListFor(model => model.HighPriority, new SelectList(
    new[] 
    { 
        new { Value = "Yes", Text = "Yes" },
        new { Value = "No", Text = "No" },
    },
     "Value",
     "Text",
    Model
))

        @Html.ValidationMessageFor(model => model.HighPriority)
    </div><br />

    <div class="editor-label">
        @Html.LabelFor(model => model.Comments)
    </div>
    <div class="editor-field">
        @Html.TextAreaFor(model => model.Comments)
        @Html.ValidationMessageFor(model => model.Comments)
    </div><br />

      <div class="editor-label">
        @Html.LabelFor(model => model.Status)
    </div>
    <div class="editor-field">
           @Html.DropDownListFor(model => model.Status, new SelectList(
    new[] 
    { 
        new { Value = "In Progress", Text = "In Progress" },
        new { Value = "Completed", Text = "Completed" },
        new { Value = "Not Started", Text = "Not Started" },
        new { Value = "Stopped", Text = "Stopped" },
    },
     "Value",
     "Text",
    Model
))
        @Html.ValidationMessageFor(model => model.Status)
    </div><br />

    <p>
        <input type="submit" value="Save" />
    </p>
</fieldset>

} }

CameraInfo.cs CameraInfo.cs

       [HttpGet]
    public ActionResult Edit(int id = 0)
    {
        Job job = db.Jobs.Find(id);

        ViewBag.LocationID = new SelectList(db.Locations, "LocationID", "LocationName", job.LocationID);


        return PartialView("_CameraInfo", job);
    }


    [HttpPost]
    public ActionResult Edit(Job job)
    {
        if (ModelState.IsValid)
        {
            var LastUpdated = System.DateTime.Now;
            job.LastUpdated = LastUpdated;
            db.Entry(job).State = EntityState.Modified;
            db.SaveChanges();
        }
        ViewBag.LocationID = new SelectList(db.Locations, "LocationID", "LocationName", job.LocationID);

        return View(job);
    }

If it is an AJAX call, the user is not supposed to be navigating. 如果它是AJAX调用,则不应引导用户。 It is very likely that the offending code is in the view, possibly a form that is POSTing to the URL instead of performing an AJAX call? 很有可能在视图中出现了令人讨厌的代码,可能是一种将其发布到URL而不是执行AJAX调用的形式?

EDIT: Now that you've shown the View code, my suspicions are confirmed. 编辑:现在您已经显示了查看代码,我的怀疑得到证实。 You should not use a regular form, since that will make the user navigate when POSTing the values. 您不应该使用常规形式,因为这样会使用户在发布值时进行导航。 An alternative to this is using AjaxForms instead of regular forms. 替代方法是使用AjaxForms代替常规表单。 Another one is preventing the form from submitting and rather submitting the values yourself through JavaScript. 另一个是阻止表单提交,而是阻止自己通过JavaScript提交值。

I'd actually advice on that last one: using JavaScript to submit, but keeping the form. 我实际上对最后一个建议:使用JavaScript提交,但保留表单。 As such, if someone in your target audience could have JavaScript disabled, you could design around that by using the regular POSTs and user navigations. 这样,如果目标受众中的某人可能禁用了JavaScript,则可以使用常规POST和用户导航来对此进行设计。 If they do, you can avoid the navigation by returning false on the submit event of the form. 如果这样做,可以通过在表单的Submit事件上返回false来避免导航。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 编辑后保留在同一视图上 - After edit stay on the same view 单击后在同一页面的特定div中加载我的局部视图 - Load my partial view in a specific div at the same page after a click 在同一页面上呈现局部视图 - render partial view on same page 将部分视图模型发布到同一MVC页面 - Posting a Partial View Model to the Same MVC Page 如何在会话结束前一直停留在同一页面上? - How to stay on the same page until session expires? 如何与 MVC 中的 HttpPost 方法保持在同一页面上? - How to stay on the same page with HttpPost method in MVC? Html.BeginForm之后如何保持在同一页面上而无需刷新或重新加载 - How to stay on the same page after Html.BeginForm without refresh or reload 在MVC 3中,我有一个包含3个部分视图的视图。 我如何使第三部分视图显示在同一页面上? - In MVC 3 I have a View with 3 Partial Views. How do i get the 3rd partial view to display on the same page? 如何防止部分视图中的脚本多次加载并在同一页面中多次使用部分时导致错误 - How to keep script in partial view from loading more than once and causing errors when partial is used more than once in the same page 如何创建页面导航局部视图? - How to create a page navigation partial view?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM