MVC3传递信息 [英] MVC3 passing information

查看:73
本文介绍了MVC3传递信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个尝试开发MVC3应用程序的新手.我需要在网页上放置一个用户控件.我知道MVC3中的用户控件现在是局部视图.我已将该控件编写为局部视图,并使用@ Html.Partial("_ TimePanel")将其添加到网页中,它似乎可以正常工作.

我遇到的问题是在客户端.主视图需要从局部视图中收集数据,并将其与自己的数据组合以发送回控制器.

I am a newbee trying to develop a MVC3 application. I needed to put a user control on a webpage. I understand that user controls in MVC3 are now partial views. I have written the control as a partial view and added it to the web page with @Html.Partial("_TimePanel") and it seems to work perfectly.

The problem I have is on the client side. The main view needs to collect data from the partial view and combine it with its own data to send back to the controller.

How do I use jscript on the parent view to collect data from the partial view?

推荐答案

好, usercontrol 部分视图是不常见的概念.两者都呈现为html,并且都嵌入在页面中,仅此而已.如果尝试将部分视图视为用户控件,则会误导自己.那就是你所做的!客户端上的视图只是html(和js),因此视图无法从其中的部分视图收集数据.视图概念仅存在于服务器端.最后只有一页.我很确定您的逻辑设计存在一些缺陷.
实际上,您可以在客户端执行所需的操作,但这没有任何意义.

假设您的两个视图都呈现为表单,并且您想要从由两种不同表单呈现的输入字段中获取数据,并将其发送到一个动作中.选项一:使用jquery使用所需的所有字段来构建ajax调用.选项二:使用jquery来构建一个包含所有字段的表单,并将其提交给操作.最佳选择:重写部分视图,而不是仅输出字段,而不输出表单,然后将具有必要操作的表单元素放入主视图"中.

请记住:使用mvc,您可以完全控制html代码,而无需遵循经典的asp.net概念.
Well, usercontrol and partial view are not common concepts. Both render to html and both are embedded in a page, but that''s all. If you try to treat a partial view as a user control, you mislead yourself. And that''s what you did! The view on client side is just html (and js), thus a view can not collect data from the partial views in it. The view concept exists only on server side. At the end there is only a page. I am pretty sure your logical design has some flaws.
Actually you can do what you want on client side, but it makes few sense.

Let''s suppose your two views are rendered to forms, and you want to get data from the input fields rendered by two different forms, and send it to one single action. Option one: use jquery to build an ajax call with all fields you need. Option two: use jquery to build a form with all fields, and submit it to the action. Best option: rewrite your partial views not to output a form, just the fields, and put the form element with the necessary action in the "main view".

Keep in mind: with mvc you have full control over the html code, you don''t need to stick to the classic asp.net concepts.


这篇关于MVC3传递信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆