MVC 4 - More Elegant way to Edit Variable-Length L

2019-03-11 06:58发布

问题:

The best advice I found for editing a variable-length list of items was written for ASP.Net MVC 2 in 2008.

http://blog.stevensanderson.com/2008/12/22/editing-a-variable-length-list-of-items-in-aspnet-mvc/

Is that approach still the best one for ASP.Net MVC 4, or is there a newer solution that is either standardized, or more elegant?

回答1:

Take a look at http://knockoutjs.com/ it's a JavaScript framework that relies on the MVVM pattern. Basically you can data-bind array models to your HTML and then post them back as JSON arrays to your controller. One great side-effect is that it helps to make your controllers more unit-test friendly (because your controllers mainly return JSON).

There's a great video about knockout at http://channel9.msdn.com/Events/MIX/MIX11/FRM08

Basically what you want to look at is observable arrays. http://knockoutjs.com/documentation/observableArrays.html

I would also highly recommend you watch the 20 minute video introduction on knockout.js. Even though it's an older video it gives you a good idea of how it works, it helped me a lot. http://channel9.msdn.com/Events/MIX/MIX11/FRM08

You can also use AngularJS http://angularjs.org/ which is the same concept (MVVM), it is put out by google. There are a lot of differences between the two but the most major one (I noticed atleast) is in angular there is more reliance on templating, in a lot of respects it gives it more power.

Between the two I think knockout is easier to get up and running but you have more options with AngularJS.