0

Good afternoon! I have the following controller action:

[HttpPost]
public ActionResult ShowComparisonResultSimpleViewFromJs(List<GetDocumentList_Result> documents,
        GetAgreementNumberKktShiftInfo_Result infoResult)
{
    ViewBag.DisplayName = CurrentUser?.DisplayName;
    ViewBag.Documents = documents;
    ViewBag.DocumentCount = Convert.ToString(documents.Count());

    return View("ComparisonResultSimpleView", infoResult);  
}

I also have an InputKktShiftView view that has a button click handler. In it I need to call the controller method specified above, as a result, the ComparisonResultSimpleView page is loaded. Here is an example of a button click handler:

<script>
function OnClick(s, e) {
    //Data for example:
    var parameterModel = {
        FnFactoryNumber: '1',//'9280440300664345',
        ShiftNumber: 38
    };

    $.ajax({
        type: 'POST',
        url: '/TaxcomProblems/GetInputKktShiftJsonResult',
        data: parameterModel
    }).success(function(data) {

        if (data.IsValid) {
            if (data.InfoResult != null) {

                var jsData = {
                    documents: data.Documents,
                    infoResult: data.InfoResult
                };

                //Here you need to call the /TaxcomProblems/ShowComparisonResultSimpleViewFromJs method

                //$.ajax({
                //    type: 'POST',
                //    url: '/TaxcomProblems/ShowComparisonResultSimpleViewFromJs',
                //    data: jsData,
                //    dataType:'html',
                //    success: function(result) {
                //        var view = $("ComparisonResultSimpleView");
                //        view.html(result);
                //    }
                //});

            } else {
                dxConfirm("Перейти на страницу выбора ККТ?")
                    .success(function() {
                        window.location.href = "/TaxcomProblems/ShowChoiceKktView";
                    });
            }
        }
    });

}

Here is the code for my ComparisonResultSimpleView page:

@using System.Web.UI.WebControls
@using BonusProgramAPI.Tools
@model BonusProgramAPI.EF.GetAgreementNumberKktShiftInfo_Result

@{
   ViewBag.Title = "Результаты для ККТ и смены";
   ViewBag.agreementNumber = Model?.agreementNumber;
   ViewBag.outletId = Model?.outletId;
   ViewBag.fnFactoryNumber = Model?.fnFactoryNumber;
   ViewBag.openDateTime = Model?.openDateTime.ToString("dd-MM-yyyy hh:mm:ss");
   ViewBag.shiftNumber = Model?.shiftNumber;
}

<script>
   function OnBtnGetKktInfoClick(s, e) {
      pcKktInfo.Show();
   }
</script>


@Html.DevExpress().FormLayout(settings =>
{
   settings.Name = "rootLayout";
   settings.Items.AddGroupItem(group =>
   {
     @* some code *@
   }
}).GetHtml()

@* PopupControls: *@
@{
    Html.RenderPartial("PopupControlKktInfoPartialView");
    Html.RenderPartial("PopupControlShiftInfoPartialView");
 }

Question: How do I call the ShowComparisonResultSimpleViewFromJs method of the TaxcomProblems controller so that the ComparisonResultSimpleView page displays? P.S.: If I use ajax, then I call the specified method correctly (all data is transferred to it correctly), but the specified page is not displayed, and the current page remains.

2 Answers 2

1

Ajax post will not refresh the page. You have to do it by yourself. Also you are sending the full page from the ShowComparisonResultSimpleViewFromJs action with the following code:

return View("ComparisonResultSimpleView", infoResult);

What you can do is make the ShowComparisonResultSimpleViewFromJs action return partial content (html) instead of full page content. And then add the returned data in a div on the success method of the ajax call.

To return partial view write:

return PartialView("~/views/ComparisonResultSimpleView.cshtml", infoResult);

Also in the javascript codes note that the element selector is missing a class or Id notation. You can simply write:

$("#ComparisonResultSimpleView").html(result);    
Sign up to request clarification or add additional context in comments.

Comments

1

Your jquery selector is attempting to get the element

<ComparisonResultSimpleView></ComparisonResultSimpleView>

from here

success: function(result) {
                     var view = $("ComparisonResultSimpleView");
                     view.html(result);
                  }

Do you mean to select the element with id = ComparisonResultSimpleView ?

<div id="ComparisonResultSimpleView"></div>

You will need to add "#" to your selector like so:

success: function(result) {
                     var view = $("#ComparisonResultSimpleView");
                     view.html(result);
                  }

Want to know if the selector is correct? Run the following line in your page's browser console

$("#ComparisonResultSimpleView").length; // greater than zero means the number of elements matching selector. Zero means no match.

12 Comments

Thanks for the answer. I added the ComparisonResultSimpleViev page code above. It is written mainly using the elements of DevExpress MVC. I wonder where to add the div element?
I commented out the ajax code in the OnClick method of the InputKktShiftView page, which should call the ComparisonResultSimpleView page, because I don’t know if this is the way to solve the problem or not.
Earned. As I understand it, you need to add a div to the InputKktShiftView page. That is, I insert the code of the ComparisonResultSimpleView page into the InputKktShiftView page, and the code of the latter is obtained as PartialView.
Question: is there another way to call another page from the current page in the JavaScript?
If it is simply another page, you can call: window.location.href = "example.com/myotherpage"; or if in the same website, use it relatively window.location.href = "/myotherpage".
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.