将数据从用户控件传递到控制器
我是 MVC 新手,正在尝试一些东西,但陷入了中间的困境。
我有一个用户控件,我有三个文本框 html 类型(ID、姓氏、名字)和一个提交按钮。
我设置按钮就像
<input type="button" value="Search"
onclick="location.href='<%= Url.Action("action", "controller") %>'" />
我在某些视图上调用此用户控件一样,
<%= Html.Partial("ucName") %>
现在按下该按钮(在用户控件上),我需要将数据从这些文本框再次传递到控制器到某些特定操作(Http Post 操作)。通过使用这些数据,我想进行一些数据库交互并将结果存储在数据集中,并将该数据集再次传递到同一视图以显示在某些网格中。
我知道传统 Asp.net 中的第一部分可以通过委托引发事件来完成,但不知道如何在 MVC 中做到这一点。
I am new to MVC, and trying something and got stuck somewhere in between.
I have a user control there I have three textbox html type(ID, Lastname, firstname) and a submit buttom.
I set the button like
<input type="button" value="Search"
onclick="location.href='<%= Url.Action("action", "controller") %>'" />
I have called this usercontrol on some view through
<%= Html.Partial("ucName") %>
Now on pressing that button(on user control) I need to pass the data from these textboxes to controller again to some specific action(Http Post action). By using this data I want to do some database interaction and storing the result in a dataset and pass this data set to same view again to show up in some Grid.
I know the first part in conventional Asp.net can be done by raising the event through delegate but don't know how to do that in MVC.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在你的actionresult中你应该有;
摆脱按钮并使用提交按钮,然后使用 BeginForm,以便它将提交到相同的操作和控制器。
MyModel 应包含三个字段,并且您的模型也应继承自 MyModel,以便 MVC 知道从哪里获取数据。
您是否已经浏览过 NerdDinner 示例,因为听起来您还没有。您需要首先这样做才能了解 MVC 模型和视图的工作原理。
In your actionresult you should have;
Get rid of the button and use a submit button instead and then use BeginForm so that it will submit to the same action and controller.
MyModel should contain the three fields and your model should inherit from MyModel as well so that MVC knows where to get the data from.
Have you gone through the NerdDinner sample yet because it sounds like you haven't. you need to do that first to get an appreciation of how MVC models and view work.
难道您不能简单地使用
submit
按钮将数据发布到所需的控制器操作吗:如果您希望在网址。
Can't you simply use a
submit
button that will post data to the desired controller action:You could also use
FormMethod.Get
if you want the parameters to be passed in the url.