How can i show partial view with tuple?

115 Views Asked by At

I'm using a partial view in my program. How can i assign this partial to a value tuple and passing to my views in asp.net mvc 5? this is my code but doesn't work

my controller

var op = checkBook.DeleteCheckDetail(detailToken, param.CheckBookToken); 
                            if (op.Success)
                            {
                                var checkDetail = checkBook.GetCheckDetail(detailToken).ToList();
                                var partial = PartialView("Partial/CheckDetailList/_Default",checkDetail);
                                var T = Tuple.Create(op, partial);
return Json(T)
                            }
                            return Json(op);

my view

 $.ajax({
                url: sendingUrl,
                type: "POST",
                data: model,
                processData: false,
                contentType: false,
                success: function (result) {
                    if (result.Item1.Success) {
                        $("#checkDatailList").html(result.Item2);
                        toastr.success(result.Item1.Message);
                    }
                    else {
                        toastr.warning(result.Item1.Message);
                    }
                },
2

There are 2 best solutions below

0
خشایار اخوت On BEST ANSWER

I had to render the partial page.

 var op = checkBook.DeleteCheckDetail(detailToken, param.CheckBookToken);
                            if (op.Success)
                            {
                                var checkDetail = checkBook.GetCheckDetail(detailToken).ToList();
                                var partial = Utility.RenderViewToString(ControllerContext, "Partial/CheckDetailList/_Default", checkDetail, true);
                                var T = Tuple.Create(op, partial);
                                return Json(T);
                            }
                            return Json(op);

And RenderViewToString Is :

  public static string RenderViewToString(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;
    }
0
S.Salek On

You can use this:

Controller:

var op = checkBook.DeleteCheckDetail(detailToken, param.CheckBookToken);

var result = new 
{ 
     isSuccess = op.Success,
     message = op.Message,
     html = op.Success ? await RenderToStringAsync("_Default", checkBook.GetCheckDetail(detailToken).ToList()) : string.Empty
};

return Json(result);

View:

 $.ajax({
            url: sendingUrl,
            type: "POST",
            data: model,
            processData: false,
            contentType: false,
            success: function (result) {
                if (result.isSuccess) {
                    $("#checkDatailList").html(result.html);
                    toastr.success(result.message);
                }
                else {
                    toastr.warning(result.message);
                }
            },