Click here to Skip to main content
15,884,836 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi

I have created a SQL command as below
C#
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);

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.
Posted
Updated 20-Jul-15 20:39pm
v2
Comments
F-ES Sitecore 3-Aug-15 5:17am    
Passing data from view to controller is quite a basic aspect of mvc. Rather than trying to learn things from scratch on a forum you'd be better going through a book on MVC and gaining a stronger grasp of the basics so that issues like this won't be a problem.

1 solution

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


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

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


value variable would hold cat in this case.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900