简体   繁体   English

ASP.Net MVC客户端和服务器端计算

[英]ASP.Net MVC Client Side and Server Side Calculations

I am new to ASP.Net MVC and am trying to maintain the correct use of the MVC development pattern. 我是ASP.Net MVC的新手,正在尝试维护MVC开发模式的正确使用。 I am getting a bit lost in the mix with all the different technologies at play. 我在使用所有不同技术的过程中迷失了方向。

Basically I have form that is strongly typed against a model. 基本上,我的表格是针对模型强类型化的。 For simplicity sake lets say the model has three ints in it. 为了简单起见,可以说该模型中包含三个整数。 Int1, Int2 and Total. Int1,Int2和总计。 I want to create a form that allows the user to enter values in Int1 and Int2 and then immediately update the Total field that is displayed to the user whenever either one of them changes. 我想创建一个窗体,该窗体允许用户在Int1和Int2中输入值,然后在其中任何一个更改时立即更新显示给用户的总计字段。 I then want to allow the user to hit save if they are happy with the total and HTTP Post back to the controller all three fields to be saved to a database. 然后,如果他们对总数感到满意,并且希望将所有三个字段都保存到数据库中,则我希望允许用户点击“保存”。

Now I went down the path of using Jquery OnChange() to do the calculations and set the value of the total which works. 现在,我走了使用Jquery OnChange()进行计算并设置总有效值的途径。 However, I feel as though I am getting away from true MVC here as I am doing calculations within my view. 但是,我觉得自己在进行视图内的计算时似乎正在远离真正的MVC。

How would things like Knockout and JSON play in here? 像Knockout和JSON这样的东西会在这里播放吗? I want to stay to the best practices of MVC as much as possible. 我想尽可能地遵循MVC的最佳做法。

Knockout and JSON would still be posting back to the server. 淘汰赛和JSON仍将回发到服务器。 Doing things client-side is encouraged, as it makes your application more responsive. 鼓励在客户端执行操作,因为这会使您的应用程序具有更高的响应速度。 You should only post to the server if you absolutely have to. 仅在绝对必要时才应发布到服务器。 I tend to make it a point to do however much I can client-side, to a certain degree. 在一定程度上,我倾向于将客户端方面的工作做得很重要。

MVC only deals with sending the requested webpage to the user's web browser. MVC仅处理将请求的网页发送到用户的Web浏览器。 Once the browser has received it, MVC will not enter the picture again, unless you send a new web request (whether by page redirect, ajax call, or any other means to launch a web request). 浏览器收到图片后,除非您发送新的Web请求(无论是通过页面重定向,ajax调用还是任何其他方式启动Web请求),否则MVC将不会再次输入图片。

Anything that happens in the browser, is not part of MVC. 浏览器中发生的任何事情都不属于MVC。 That doesn't mean you should steer clear from client-based interactions! 这并不意味着您应该避开基于客户端的交互! It only means that you'll be using something other than MVC to provide that part of the user experience. 这仅意味着您将使用MVC以外的其他功能来提供那部分用户体验。

There are plenty of javascript frameworks to choose from if you have a sizeable need for a client-side smooth user experience. 如果您非常需要客户端流畅的用户体验,则可以选择很多javascript框架。

But for the example of having a total calculated when the user enters separate values, that can easily be solved by javascript, and perhaps preferably jQuery. 但是对于在用户输入单独的值时计算总数的示例,可以很容易地用javascript(最好是jQuery)解决。

But you shouldn't worry about moving away from the MVC structure. 但是您不必担心会脱离MVC结构。 It's not supposed to be an all-encompassing framework. 它不应该是一个包罗万象的框架。 There's a beautiful synergy between ASP.Net MVC and javascript/jQuery, and I can only encourage such an approach. ASP.Net MVC与javascript / jQuery之间有着美丽的协同作用,我只能鼓励这种方法。

Long story short: you're on the right track. 长话短说:您走在正确的轨道上。 No reason to doubt yourself. 没有理由怀疑自己。

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

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