0

in Html

@section Scripts
{
<script type="text/javascript">

    $(document).ready(function() {

        $('#bSubmit').click(function(event) {
            event.preventDefault();

            var formData = new FormData();
            formData.append('@Html.Get__RequestVerificationTokenString()', '@Html.GetAntiForgeryToken()');
            formData.append('request.MessageId', @Model.MessageId);
            formData.append('request.Message', $('#Message').val());

            var xmlHttpRequest = new XMLHttpRequest();
            if (!window.XMLHttpRequest) {
                xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
            }

            xmlHttpRequest.open("POST",
                '@Url.Action("Send", "Page", new {area = "Question"})',
                true);

            xmlHttpRequest.onloadend = function() {
                var response = JSON.parse(xmlHttpRequest.responseText);

                $('#prButton').removeClass("visible");
                $('#prButton').addClass("invisible");

                switch (response.Type) {
                case 10:
                    LOG(response.PartialView);

                    $('#layoutSingleMessageItems .card-box').replaceWith(response.PartialView);
                    break;
                case -13:
                    @Html.ShowAlert("خطا در ارسال اطلاعات!");
                    break;
                }
            };

            xmlHttpRequest.send(formData);

        });

    });

</script>
}
<div class="row">

    <div class="col-md-7">
        @Html.Partial("Par_SingleMessageItem", Model.SingleMessageItems)
    </div>

<div class="col-md-5">
    <div class="card-box">
        <form>
            @Html.AntiForgeryToken()
            <div class="form-group col-md-12">
                <label class="col-form-label">پاسخ</label>
                <textarea rows="8" id="Message" placeholder="لطفا پاسخ خود را وارد کنید ..."></textarea>

            </div>

            <div class="form-group col-md-12 m-t-10">

                <button id="bSubmit" type="submit">
                    ارسال پاسخ

                </button>
            </div>

        </form>

    </div>
</div>

in PartialView (Par_SingleMessageItem)

@model List<_4Wheels.Areas.Question.Models.SingleMessage.SingleMessageItem>

<div id="layoutSingleMessageItems" class="card-box">
    <div class="card-box">
        @{
            foreach (var item in Model)
            { 
                //code
            }
         }
   </div>
</div>

in Controller

[HttpPost]
public ActionResult Send() {
   /*
      items = list object
   */

   return Json(new SendAnswerResponse {
       Type = 10,
       SingleMessageItems = PartialView("Par_SingleMessageItem" , items)
   });
}

in SendAnswerResponse

public class SendAnswerResponse : MvcResponse
{
    public PartialViewResult SingleMessageItems { get; set; }
}

All this code works fine but $('#layoutSingleMessageItems .card-box'). replaceWith(response.PartialView); It doesn't work, it doesn't make any errors Returns the desired PartialView just the tag is not updated.

I even put it in Console.log (response.PartialView) to display this message

Model: (5) […] ​​ 0: Object { Message: "fdgdfgdfg", Username: "", MessageDate: "1398-08-06 23:01:11", … } ​​ 1: Object { Message: "dsafd", Username: "", MessageDate: "1398-08-07 08:34:19", … } ​​ 2: Object { Message: "vcgdsafd", Username: "", MessageDate: "1398-08-07 08:34:58", … } ​​ 3: Object { Message: "fdgfdg", Username: "", MessageDate: "1398-08-07 08:42:43", … } ​​ 4: Object { Message: "fdgfdg", Username: "", MessageDate: "1398-08-07 08:42:54", … } ​​ length: 5 ​​ : Array [] ​ TempData: Array [] ​ View: null ​ ViewBag: Object { } ​ ViewData: [] ​​ length: 0 ​​ : Array [] ​ ViewEngineCollection: Array [ {…} ] ​ ViewName: "Par_SingleMessageItem" ​ : {…} ​​ defineGetter: function defineGetter() ​​ defineSetter: function defineSetter() ​​ lookupGetter: function lookupGetter() ​​ lookupSetter: function lookupSetter() ​​ proto: ​​ constructor: function Object() ​​ hasOwnProperty: function hasOwnProperty() ​​ isPrototypeOf: function isPrototypeOf() ​​ propertyIsEnumerable: function propertyIsEnumerable() ​​ toLocaleString: function toLocaleString() ​​ toSource: function toSource() ​​ toString: function toString() ​​ valueOf: function valueOf() ​​ : function proto() ​​ : function proto() Utility.js:13:13

2 Answers2

0

What does the response.SingleMessageItems return ? It returns html ? or a data structure? I think you try something like this

for(var i =0; i< response.SingleMessageItems.length; i++)
{
  $('#layoutSingleMessageItems .card-box').append(response.SingleMessageItems[i]);
}
Joshi
  • 36
  • 1
  • 4
  • This code didn't work either. I edited my post to see it again. –  Oct 28 '19 at 21:50
  • I'm sending a PartialView but it doesn't replace it in the tag. –  Oct 28 '19 at 21:51
  • I think you should replace the content of the partial view, not trying to replace in $('#layoutSingleMessageItems .card-box') a whole partial view, in the code above you replace the content, using append, html, or load. https://stackoverflow.com/questions/11122268/updating-partial-view-with-jquery-in-asp-net-mvc-c-sharp – Joshi Oct 29 '19 at 13:01
0

Updated with the following PartialView code

public static string RenderPartialViewToString(ControllerContext context, string viewPath, object model = null, bool partial = false)
        {
            // first find the ViewEngine for this view
            ViewEngineResult viewEngineResult = null;
            if (partial)
                viewEngineResult = ViewEngines.Engines.FindPartialView(context, viewPath);
        else
            viewEngineResult = ViewEngines.Engines.FindView(context, viewPath, null);

        if (viewEngineResult == null)
            throw new FileNotFoundException("View cannot be found.");

        // get the view and attach the model to view data
        var view = viewEngineResult.View;
        context.Controller.ViewData.Model = model;

        string result = null;

        using (var sw = new StringWriter())
        {
            var ctx = new ViewContext(context, view, context.Controller.ViewData, context.Controller.TempData, sw);
            view.Render(ctx, sw);
            result = sw.ToString();
        }

        return result;
    }

    public static T CreateController<T>(RouteData routeData = null) where T : Controller, new()
    {
        // create a disconnected controller instance
        T controller = new T();

        // get context wrapper from HttpContext if available
        HttpContextBase wrapper;
        if (System.Web.HttpContext.Current != null)
            wrapper = new HttpContextWrapper(System.Web.HttpContext.Current);
        else
            throw new InvalidOperationException("Cannot create Controller Context if no active HttpContext instance is available.");

        if (routeData == null)
            routeData = new RouteData();

        // add the controller routing if not existing
        if (!routeData.Values.ContainsKey("controller") &&
            !routeData.Values.ContainsKey("Controller"))
            routeData.Values.Add("controller", controller.GetType().Name.ToLower().Replace("controller", ""));

        controller.ControllerContext = new ControllerContext(wrapper, routeData, controller);
        return controller;
    }

And to Used

return Json(new SendAnswerResponse {
   Type = 10,
   SingleMessageItems = MvcHelper.RenderPartialViewToString(
                    MvcHelper.CreateController<Controller Name>().ControllerContext ,
                    "view Path", items , true)
});

https://stackoverflow.com/a/42559538/11657764