是否有一种本机方法可以创建客户端模型以发布到我的控制器而无需手动滚动它们?
我对“现实世界”MVC / .NET 开发相当陌生(我从 MVC 1 开始就一直在研究 MVC,过去 10 年我一直在基于 Web,所以我也不是真正的菜鸟)希望有一些我错过的东西..我正在做很多手工滚动 JSON 将模型传递回控制器,这已经变得越来越费力(让我回到 2005 年哈哈)所以我觉得一定有更好的方法..也许像 Backbone 这样的东西?只是为了客户端建模?我假设有某种 .toJSON() 或某处的东西..无论如何,这是我正在谈论的一个简单示例:
目前我正在使用视图模型结构将复杂的数据类型/列表等绑定到查看页面..这太棒了..然后我通过JS构建小表单或“微模型”来处理小变化/用户与页面的交互..即“网格”中发布id的删除按钮到控制器被删除..大部分工作正常。 (我在这里非常通用)
现在,当我处理具有一定深度的模型时,说:
ViewModel1{
string Property1
int Property2
List<MicroModel>
}
MicroModel{
string name
string description
}
我有一个用于更新 MicroModel 的控制器..说:
ActionResults testController(List<MicroModel> micromodel)
我必须在客户端构建一些 JSON 字符串要创建一个代表预期输入的模型,一旦我构建了这个字符串,我就会通过 JQuery $.ajax() 发布它,控制器将拾取键入的模型(顺便说一句,这很酷)..但是...
<强>我真的需要吗手卷这些客户端对象以镜像控制器期望的对象?
我不想直接使用表单绑定到模型,我也不会发布 formCollection 我如何使用对象。在客户端以一种很好的现代方式,然后将数据传递给控制器?
感谢您的反馈。谢谢您的时间。
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)
NOW, 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Knockout.js http://knockoutjs.com 及其声明性绑定及其映射插件 http://knockoutjs.com/documentation/plugins-mapping.html 将反序列化 JSON 是一个好的起点。
另一个额外的好处(也许是目的)是在客户端上使用模型-视图-视图模型 (MVVM) 模式。
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.