简体   繁体   English

通过Knockout使用MVVM的asp.net MVC,将模型传递给j​​avascript

[英]asp.net MVC with MVVM via Knockout, pass model to javascript

I'm writing an MVC application and I've decided that Knockout.js will help with a lot of my issues, except in the process, it has created a new issue that I'm having a problem solving. 我正在写一个MVC应用程序,我已经决定Knockout.js将帮助解决我的很多问题,除了在这个过程中,它已经创建了一个我正在解决问题的新问题。

I converted all my MVC endpoints to just return views, and then I've created several other endpoints that returns the JSON objects. 我将所有MVC端点转换为仅返回视图,然后我创建了几个返回JSON对象的端点。 When the user browses to the website, the site loads the basic UI frame. 当用户浏览网站时,该网站会加载基本UI框架。 After that, Knockout.js loads the model via AJAX calls. 之后,Knockout.js通过AJAX调用加载模型。 This has caused every page to load, and then "load" again where as with just the MVC the page loaded and all the data was present. 这导致每个页面都被加载,然后再次“加载”,就像加载页面的MVC和所有数据一样。

I've been looking into ways to get both to load at the same time, but I haven't found anything that doesn't require me to completely refactor the site. 我一直在研究如何同时加载两者,但我没有找到任何不需要我完全重构网站的东西。 The best option was: 最好的选择是:

  • Merge the JSON endpoints into the view endpoints and convert the model to JSON in the codebehind using @Html.Raw(Json.Encode(object)) 将JSON端点合并到视图端点,并使用@Html.Raw(Json.Encode(object))将模型转换为代码隐藏中的JSON

but then I have to get ride of all my pure JSON endpoints. 但是我必须得到所有纯JSON端点。

Is there anything else I can do to pass JSON endpoint data to the view on the serverside? 我还能做些什么来将JSON端点数据传递到服务器端的视图吗? Or any way to get the model and the view to load at the same time? 或者以任何方式同时加载模型和视图?

Your architecture is correct, but you want the initial load to pre-populate your views with data from the model. 您的体系结构是正确的,但您希望初始加载使用模型中的数据预先填充视图。 Within the controller action that just returns the Views, you should put data to populate in ViewData. 在刚刚返回视图的控制器操作中,您应该将数据放入ViewData中。 Then the views are initially loaded with Model values, and each UI element can have an AJAX update that invokes the Controller action for that specific field. 然后,视图最初使用Model值加载,并且每个UI元素都可以具有AJAX更新,该更新调用该特定字段的Controller操作。

This would also be an ideal case for a ViewModel . 这也是ViewModel理想情况 This may help as well . 这可能有助于以及

I also agree that it's a good idea to let your initial view generation render the initial data right into the view. 我也同意让初始视图生成将初始数据直接渲染到视图中是个好主意。 In general this does yield better performance than a separate AJAX call + JavaScript populating the data afterwards. 通常,这会产生比单独的AJAX调用+之后填充数据的JavaScript更好的性能。

But I also think this really depends on the type of page you are loading. 但我也认为这取决于您正在加载的页面类型。 If your app is primarily AJAX based (SPA style model) then that first load is probably not that important because it happens only once, and it's probably OK to have that take a little extra time. 如果您的应用程序主要是基于AJAX(SPA样式模型),那么第一次加载可能不是那么重要,因为它只发生一次,并且可能需要花费额外的时间。 It is somewhat easier and more consistent to have all your data loaded one way either via AJAX or via traditional View manipulation. 通过AJAX或传统的View操作以一种方式加载所有数据会更容易一些。 And when your client side code requires you to re-load the entire set of data for a list or a record you don't end up duplicating the template/view logic to generate that output. 当您的客户端代码要求您重新加载列表或记录的整个数据集时,您最终不会复制模板/视图逻辑以生成该输出。

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

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