开发者

Is there a native way to create Client-Side models to Post to my controllers without handrolling them?

I'm fairly new to "real world" MVC / .NET development (i've been studying MVC since MVC 1, and i've spent the last 10 years web based so i'm not really a noober either) I'm hoping there's something out there that i'm missing though.. i'm doing a lot of hand rolling JSON to pass models back to the controller which has become more and more painstaking (takes me back to 2005 lol) so i'm feeling like there must be a better way.. maybe something like Backbone? just for the client side modeling? I'm assuming there's some kind of .toJSON() or something somewhere.. anyhow, here's a quick example of what i'm talking about:

Currently i'm using a viewmodel structure to bind complex data types / lists, etc to the view page.. which is fantastic.. then i'm building either little forms or "micro-models" through JS for handling little changes / user interaction with the page.. i.e. a remove button in a "grid" that posts the id to the controller to be removed.. for the most part that works fine. (I"m being very generic here)

N开发者_如何学PythonOW, when i'm dealing with a model with some depth, say:

 ViewModel1{
    string Property1
    int Property2
    List<MicroModel>
 }

 MicroModel{
    string name
    string description
}

and i have a controller for updating the MicroModel.. say:

 ActionResults testController(List<MicroModel> micromodel)

I have to do some JSON string building on the client side to create a model that represents the expected input, once i've built this string i POST it via JQuery $.ajax() and the controller will pick up the typed model (which is waaaay cool btw).. BUT...

Do i really need to handroll these client side objects to mirror my objects the controller is expecting?

I'd prefer to not work with form binding directly to the model, i'm not going to post a formCollection either. How can i work with objects on the client side in a nice modern way and then pass the data to the controller?

feedback is appreciated.. thank you for the time.


Knockout.js http://knockoutjs.com and it's declarative bindings along with its mapping plugin http://knockoutjs.com/documentation/plugins-mapping.html that will deserialize JSON is a good place to start.

An added bonus, perhaps the purpose, is working with the Model-View-View Model (MVVM) pattern on the client.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜