我如何在ASP.NET MVC中获得简单的文本框回传 [英] How do i get simple textbox post back in ASP.NET MVC

查看:74
本文介绍了我如何在ASP.NET MVC中获得简单的文本框回传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我对Asp.net MVC还是陌生的,而对asp.net而言,我当时很简单的事情现在让我头疼.
基本上,我试图做一个从头开始的简单存储,并采取了在查询字符串中传递我的状态变量的路线(这是唯一的方法吗?),我有一个包含菜单和主搜索框的主页面上下文页面是所有产品清单,订单和过滤器选项.每次用户从菜单(在母版页上)上单击类别时,它将填充上下文,然后用户对该上下文执行的任何操作(例如下一页",每页项"或排序依据")将重新发布查询表示这意味着每次页面加载时,都必须从具有所有这些状态变量的模型中构建页面.
一切正常,除了我现在只停留在搜索框的简单操作上,因为它位于母版页上,并且我无法访问状态变量以传回页面所处的状态,而另一个问题是我不知道单击搜索框旁边的按钮,甚至都不知道如何从控制器的文本框中访问搜索文本.

我们将不胜感激,对此有任何帮助,也可以提出不同方法的想法
非常感谢
Lee

Hello all,

I am kind of new to Asp.net MVC and what was very simple to me in asp.net is now causing me headache.

Basically im trying to do a simple store built from the ground up and have taken the route of passing my state variables in the querystring(is this the only way?), I have a master page that contains the menu and a search box the main context page is all the product listings, order and filter options. Every time a user clicks a category from the menu (on the master page) it fills the context then any action the user does on that context such as "next page", "items per page" or "order by" get posted back in the querysttring this means that every time the page loads it has to build the page from the model that has all these state variables.
This all works fine except I''m now stuck on a simple operation of a search box in that its on the master page and i cant access my state variables to pass back what state my page is in and the other issue is i don''t even know how to access the search text from the textbox in the controller when a button is clicked next to the searchbox.

Any help on this would be greatly appreciated or ideas of different approaches
Many thanks
Lee

推荐答案

以下是结合了我所详细介绍的一些概念的答案.

确保您的搜索框具有ID.

将要表达的所有选项放在可通过脚本访问的隐藏输入中.

如果需要在客户端访问期间保持这些状态,请考虑使用会话对象(或其他有状态方法).

使用jQuery收集相关位,将它们捆绑到json对象中,然后将其提交给SearchController.

如果您的UI片段可以动态生成,请考虑使用局部视图并创建可以容纳渲染它们所需数据的类.将这些类的实例传递到partial中以呈现您的UI.

希望这会有所帮助.

干杯.
Here''s an answer incorporating some of the concepts I''ve details.

Make sure your search box has an ID.

Put any options you want to express in hidden inputs that are accessible via script.

If these need to be persistent for the duration of the clients visit, consider using the session object (or other state-ful approaches).

Use jQuery to collect the relevant bits, bundle them up into a json object and submit it to the SearchController.

If pieces of your UI can be dynamically generated, consider using partial views and creating classes that can hold the data you need to render them. Pass instances of these classes into the partial to render your UI.

Hope this helps.

Cheers.


您必须忘记对MVC中的回发,页面状态和控件的了解. MVC优雅,轻巧且易于使用,但如果您仍在考虑如何在Web表单中进行操作,则不是这样.

按钮点击事件不会免费回发.您必须将按钮置于带有动作的表单中,或者设置一个客户端处理程序以获取该值并将其发布.有MVC的方法可以做到这一点.

对于表单方法,请按照以下方式操作:在MVC中呈现表单 [ jQuery.post() [ http://jameschambers.com/articles [^ ]

一些提示:
1)您将要在控制器上创建一个动作以接受搜索条件.您可能要向该操作(HttpPost)添加一个属性.
2)如果您要传递多个值,则MVC的方法是创建一个Action上接受的SearchOptions类.模型绑定开始,并负责为您创建对象.
3)请勿尝试将事情关联到母版页/页面加载中的各种事件(在某些情况下,这很有意义,但在您的情况下则不然).您的模型应包含功能,视图代表模型,而控制器则将其绑定在一起.

请随意发布其他更具体的问题,以解决您正在从事的工作.

干杯.
You have to forget what you know about post backs, page state and controls in MVC. MVC is elegant, lightweight and simple to work with, but not if you''re still thinking about how you do it in web forms.

Button click events aren''t posted back for free. You have to either put the button in a form with an action or set up a client-side handler to take the value and post it. There are MVC ways to do this.

For the form approach, go this way: Rendering forms in MVC[^]

For the client-side script, use jQuery: jQuery.post()[^]

I have a ton of articles on my blog related to jQuery, model binding, controllers, actions, etc.: http://jameschambers.com/articles[^]

A couple of tips:
1) you''re going to be creating an action on the controller to accept the search criteria. you''ll likely want to add an attribute to that action (HttpPost).
2) if you have multiple values that you want passed it, the MVC way to do this would be to create a SearchOptions class that is accepted on the action. Model binding kicks in and takes care of creating the object for you.
3) don''t try to wire things into your master page/page load kinds of events (there are some cases where this makes sense, but not in yours). your models should contain functionality, the views represent the model and the controller bind it together.

Feel free to post additional questions more specific to what you''re working on.

Cheers.


这篇关于我如何在ASP.NET MVC中获得简单的文本框回传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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