Render MVC PartialView into SignalR response

前端 未结 6 997
抹茶落季
抹茶落季 2020-12-13 04:19

I would like to render a PartialView to an HTML string so I can return it to a SignalR ajax request.

Something like:

SignalR Hub (mySignal

6条回答
  •  再見小時候
    2020-12-13 04:47

    Probably the best choice is to use RazorEngine, as Wim is suggesting.

    public class mySignalRHub: Hub
    {
        public string getTableHTML()
        {
            var viewModel = new[] { new DataItem { Value1 = "v1", Value2 = "v2" } };
    
            var template = File.ReadAllText(Path.Combine(
                AppDomain.CurrentDomain.BaseDirectory,
                @"Views\PathToTablePartialView\_MyTablePartialView.cshtml"));
    
            return Engine.Razor.RunCompile(template, "templateKey", null, viewModel);
        }
    }
    

提交回复
热议问题