简体   繁体   English

如何在成功提交表单后更新我的部分视图?

[英]How could I update my Partial View on Successful form submission?

First of all I do apologize if this has been covered elsewhere, basically I have a help page on my website, consisting of an FAQ page (the content of which, is being served from a Web Service), and also a button which displays a partial view, this partial view takes help request information and sends it to the Server as per your "bog standard" form submission. 首先,我很抱歉如果其他地方都提到了这个问题,基本上我的网站上有一个帮助页面,其中包括一个FAQ页面(其内容由Web服务提供),以及一个显示部分视图,此部分视图将获取帮助请求信息,并按照您的“沼泽标准”表单提交将其发送到服务器。

However, I would like not to leave the page ,instead preferring to utilize AJAX to display a success message once the information has been saved. 但是,我不想离开页面,而是希望在信息保存后立即利用AJAX显示成功消息。

I have already tried returning JSON from the Controller, and retrieving that through AJAX to display on the page However, this replaces my entire page with only the JSON on it! 我已经尝试过从Controller返回JSON,并通过AJAX检索它以显示在页面上。但是,这仅用JSON替换了我的整个页面!

I'd really appreciate a push in the right direction with this, if at all possible. 如果可能的话,我真的很感激朝着正确的方向前进。

Here's my Controller Action 这是我的控制器动作

  [AllowAnonymous]
        public ActionResult Index()
        {
        FAQModel.Questions= Service.PopulateFAQQuestions();
            FAQModel.Answers = Service.PopulateFAQAnswers();


            return View("Index",FAQModel);

        }

        [AllowAnonymous]
        [HttpPost]
        public async Task <ActionResult> SendHelpRequest(RequestViewModel model)
        {
            model.RequestDate = DateTime.Now;
            await Service.SendHelpRequestAsync(model.Name, model.Details, model.RequestDate, model.Email, model.ContactNumber);

            return RedirectToAction("Index");
        }

and my Partial View 和我的局部视图

@model MyProject.Models.RequestViewModel

<script>
    function Success() {
        var Success = document.getElementById("SuccessText");

       Success.innerHTML = "Request Submitted";
    }

    function Failure() {
        var Failure = document.getElementById("SuccessText");

        Failure.innerHTML = "Request Failed";
    }
</script>

@using (Ajax.BeginForm("SendHelpRequest", "Help", null, new AjaxOptions
    {
        HttpMethod = "POST",
        OnSuccess = "Success",
        OnFailure = "Failure",
        UpdateTargetId = "SuccessText"
    }))

    {
        @Html.AntiForgeryToken()
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="modal-content">
            <div class="modal-header" style="border-bottom: none !important;">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
                <h3 class="modal-title">Support Request</h3>
            </div>

            <div class="container-fluid">
                <div class="form-group">
                    @Html.LabelFor(m => m.Name, new { @class = "col-md-2 control-label" })
                    <div class="col-md-10" style="border-top: none !important;">
                        @Html.TextBoxFor(m => m.Name, new { @class = "form-control", @id = "txtEmail", @placeholder = "E.G: Fred Flintstone" })
                        @Html.ValidationMessageFor(m => m.Name, "", new { @class = "text-danger" })
                    </div>
                </div>

                <div class="form-group">
                    @Html.LabelFor(m => m.Email, new { @class = "col-md-2 control-label" })
                    <div class="col-md-10">
                        @Html.TextBoxFor(m => m.Email, new { @class = "form-control", @placeholder = "E.G: example@example.com" })
                        @Html.ValidationMessageFor(m => m.Email, "", new { @class = "text-danger" })
                    </div>
                </div>

                <div class="form-group">
                    @Html.LabelFor(m => m.Details, new { @class = "col-md-2 control-label" })
                    <div class="col-md-10">
                        @Html.TextAreaFor(m => m.Details, new { @class = "form-control", @rows = "10", @style = "max-width:100%; max-height:100%; resize:none", @placeholder = "Please Enter Details (max 500 Characters)", @maxlength = "500" })
                        @Html.ValidationMessageFor(m => m.Details, "", new { @class = "text-danger" })
                    </div>
                </div>
                <div class="form-group">
                    @Html.LabelFor(m => m.ContactNumber, new { @class = "col-md-2 control-label" })
                    <div class="col-md-10">
                        @Html.TextBoxFor(m => m.ContactNumber, new { @class = "form-control", @rows = "10", @placeholder = "E.G:00000000000", @maxlength = "15" })
                        @Html.ValidationMessageFor(m => m.ContactNumber, "", new { @class = "text-danger" })

                    </div>
                </div>

                <div class="col-md-12">
                    <div class="modal-footer">
                        <div align="left" class="col-md-6">
                            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                        </div>
                        <div align="right" class="col-md-6">
                            <button type="submit" class="btn btn-default">Submit</button>
                        </div>
                    </div>
                </div>


            </div>
        </div>
    }

<div id =" SuccessText"></div>

Use the Insertmode "Replace" on your Ajax.BeginForm() then return a partial view that inherits the success message. 在Ajax.BeginForm()上使用Insertmode“ Replace”,然后返回继承成功消息的局部视图。

This will replace the the "SuccessText" since the TargetId is set to this. 由于将TargetId设置为此,因此它将替换“ SuccessText”。 Make sure you have added the "jquery.unobtrusive-ajax.min.js" JS library otherwise it will not work! 确保已添加“ jquery.unobtrusive-ajax.min.js” JS库,否则它将无法正常工作!

I actually managed to solve this problem myself, what happened was that my code was actually working fine; 我实际上设法自己解决了这个问题,发生的事情是我的代码实际上运行良好。 however I wasn't doing anything with the results of my AJAX call! 但是我对AJAX调用的结果没有做任何事情!

@using (Ajax.BeginForm("SendHelpRequest", "Help", null, new AjaxOptions
    {
        HttpMethod = "POST",
        OnSuccess = "Success",
        OnFailure = "Failure",
        UpdateTargetId = "SuccessText",
        InsertionMode = InsertionMode.Replace

    }))

My success method 我的成功方法

function Success(result) {
      alert( result);


    }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM