如何将视图的标题从View传递给Controller? [英] How to pass title of the link from View to Controller?

查看:64
本文介绍了如何将视图的标题从View传递给Controller?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我创建了一个SQL命令如下:

Hi

I have created a SQL command as below

SqlCommand cmd = new SqlCommand("sp_GetALLBooks", con);
cmd.CommandType = CommandType.StoredProcedure;

SqlParameter paramId = new SqlParameter();
paramId.ParameterName = "@id";
//paramId.Value = "1";
//cmd.Parameters.Add(paramId);

SqlParameter paramTitle = new SqlParameter();
paramTitle.ParameterName = "@book_title";
paramTitle.Value = "@book_title";
cmd.Parameters.Add(paramTitle);

SqlParameter book_cat = new SqlParameter();
book_cat.ParameterName = "@book_cat";
book_cat.Value = "@book_cat";
cmd.Parameters.Add(book_cat);



我有一个视图,我从数据库中获取数据并为类别创建列表。 />


当我点击任何类别时如何将其传递给book_cat.Value =@ book_cat;中的上述命令所以这将使我只能进行与该类别相关的搜索????



同样的情况是书名。


I have a view where I am fetching data from database and creating a list for "Category".

When I click any Category how can I pass that to the above Command in "book_cat.Value = "@book_cat";" so that will enable me to make search related to that category only????

The same case is with Book Title.

推荐答案

实际上,您不直接在视图中编写数据操作代码。您将控制器用于此类目的。视图必须仅用于向客户端表示数据,并且(作为最佳实践)模型必须使用控制器从视图中抽象出来。



但是,视图通过生成请求来触发控制器。现在有多种方法可以在控制器上触发动态代码。其中一个是使用QueryStrings。您可以在URL中附加文本,以便在控制器查看URL时,它会根据该值执行单独的代码。



Actually you do not write the data manipulation code directly in a view. You use a controller for such purposes. Views must only be used to represent the data to the client and (as a best practice) models must be abstracted from a view using the controller.

However, a view triggers a controller by generating a request. Now there are multiple ways in which you can trigger a dynamic code on the controller. One of them is using QueryStrings. You can append a text in the URL so that when controller looks at the URL, it executes a separate code based on that value.

http://www.example.com/controller/action?query_string=value





现在,更改 query_string value 的值来实现它适合您的需要。例如, category = cat 。控制器可以将其作为变量处理,或者您可以随时使用





Now, change the value of query_string and value to make it suitable for your need. Such as, category=cat. Controller would be able to handle it as a variable, or you can always request the value using

var value = Request.QueryString["category"]; // name of the QueryString





value 在这种情况下变量将保留cat。



value variable would hold cat in this case.


这篇关于如何将视图的标题从View传递给Controller?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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