Öncelikle View’da form oluşturmamız gerek. Çünkü arama şartlarını
bu form ile Controller’a ileteceğiz.
@using (Html.BeginForm("Index", "Home", FormMethod.Get))
{}
Burada yazdığımız “Index” = Action
“Home” = Controller’ın adı
Formmethod.Get ise; bu form ile yaptığımız işlemin bir “Get”
operasyonu olduğunu gösteriyor. Arama şartlarını
(Kişiye göre ara / Türe göre ara) Radio Buttonlar aracılığıyla seçeceğiz.
@Html.RadioButton("SearchBy", "SearchByBook", true); <text> İsme Göre</text>
@Html.RadioButton("SearchBy", "SearchByRating") <text> Puana Göre</text>
@Html.RadioButton("SearchBy", "SearchByAuthor") <text> Yazara Göre</text>
@Html.RadioButton("SearchBy", "SearchByGender") <text> Türe Göre</text>
RadioButton’un aldığı ilk parametre görüldüğü gibi “SearchBy” olarak girildi.
Yazdığımız “SearchBy”, RadioButton’un ismidir. Bu ismi,
Controller kısmında kullanacağız.
RadioButton’un ikinci parametresi de “SearchByBook”. Bu parametre ile,
kullanıcının neye göre arama yaptığını (isim,tür,puan vb) anlamış olacağız.
Bu arada üçüncü parametre olan “true” ise, bu Radiobutton’un otomatik olarak
seçili gelmesini sağlayacak. Ve bir TextBox ve “Ara” isminde bir button
oluşturalım ve kullanıcının yazdığı veriyi Server’a gönderelim.
@Html.TextBox("SearchTerm")
<input type="submit"
value="Ara!" class="buttonSearch" />
Görüleceği üzere; TextBox’un ismi “SearchTerm” olarak girilmiştir. Bu ismi kullanarak,
Controller kısmında, kullanıcının TextBox’a yazdığı ve gönderdiği veriyi elde etmiş
olacağız. Şimdi Controller kısmına geçelim.
public ActionResult Index(string searchBy, string searchTerm)
{
if (searchBy == "SearchByBook")
{
Return View(_sampleContext.Books.Where(b => b.BookName.StartsWith(SearchTerm)).ToList)
Şimdi ise View ve Controller’da tam olarak ne olup bittiğini anlayalım:
@Html.RadioButton("SearchBy", "SearchByBook", true);
Bu radiobutton un adı SearchBy, değeri ise SearchByBook’tur.
Controller kısmında:
public ActionResult Index(string searchBy, string searchTerm)
String SearchBy yazmıştık. Bu SearchBy parametresiyle; ismi SearchBy olan buttonlarımızın
“Value’sini alıyoruz” Örneğin bi üstteki radiobutton’un değeri SearchByBook idi.. Yani
kullanıcı eğer bu radiobutton’u seçer ise şayet; biz controller’daki searchBy
parametremize SearcyByBook değerini atamış oluruz. SearchBy parametresinde gerçekleşen
işlem budur. String SearchTerm ile ise; kullanıcının TextBox’a yazdığı veriyi çekmiş olduk..
@Html.TextBox("SearchTerm")
Burada gerçekleşen işlem de aynı mantık ile ilerliyor. TextBox’un adını bildiğimiz
üzere “SearchTerm” olarak girmiştik. Dolayısıyla kullanıcının TextBox’a yazmış olacağı
herhangi bir kelime veya harfi; Controllerda bulunan String SearchTerm parametresine
atamış olacağız.
public ActionResult Index(string searchBy, string searchTerm)
Yani ne yapmış olduk? Kişinin neye göre arama yapacağını (türe göre mi, isme göre mi,
yoksa puana göre mi) SearchBy parametresine atmış olduk è View kısmında radiobuttonların
ismi SearcBy idi ve bu ismi kullanarak seçilen RadioButton’un değerine ulaşmış olduk.
SearchTerm parametresine ise, yine aynı şekilde kullanıcının, SearchTerm isimli
TextBox’a yazmış olduğu veriyi, yine Controller’da bulunan String SearchTerm
parametresine atamış olduk. Ve bu parametrelere atanmış olan verileri elde ederek;
kullanıcının istediği şekilde olan arama sonuçlarını listeleterek kullanıcıya sunmuş olduk.
Return View(_sampleContext.Books.Where(b => b.BookName.StartsWith(SearchTerm)).ToList)
Kodların derlenmiş hali:
VIEW:
@using (Html.BeginForm("Index", "Home", FormMethod.Get))
{
<b>Ara:   </b>
@Html.RadioButton("SearchBy", "SearchByBook", true); <text> İsme Göre</text>
@Html.RadioButton("SearchBy", "SearchByRating") <text> Puana Göre</text>
@Html.RadioButton("SearchBy", "SearchByAuthor") <text> Yazara Göre</text>
@Html.RadioButton("SearchBy", "SearchByGender") <text> Türe Göre</text>
<br />
@Html.TextBox("SearchTerm")
<input type="submit"
value="Ara!" class="buttonSearch" />
}
CONTROLLER:
public ActionResult Index(string searchBy, string searchTerm)
{
if (searchBy == "SearchByBook")
{
return View( "abc", _myLibraryContext.Books.
Where(x=>x.BookName.StartsWith(searchTerm)).ToList());
}
if (searchBy == "SearchByRating")
{
return View("abc", _myLibraryContext.Books.
Where(n => n.Rating.ToString() == searchTerm));
}
else
{
return View("abc", _myLibraryContext.Books.ToList());
}
}
23 Nisan 2017 Pazar
11 Nisan 2017 Salı
Fully Qualified Names in SQL
We can
provide the table name just before the column name in the line. We’ll separate
each other using dot.
SELECT
customers.City FROM customers;
We can
provide more columns to the user. See:
SELECT
customers.City, customers.Address FROM customers;
-Its useful practice when working on many tables that
may have the same columns (same column names).
Link:
6 Nisan 2017 Perşembe
Sorting Multiple Columns in SQL
SELECT
*
FROM
customers
ORDER
BY
CustomerName, ContactName;
In this usage, if you have some results that has the same
CustomerName,
these results will be ordered by the ContactName
.
That’s the logic of this syntax.
http://www.w3schools.com/sql/trysql.asp?filename=trysql_select_all
http://www.w3schools.com/sql/trysql.asp?filename=trysql_select_all
LIMIT keyword in SQL
We can set a limit to the returned data. We will use LIMIT
keyword to achieve this. See:
SELECT
Country
From
customers
LIMIT
5;
We can use more than one column with the LIMIT keyword. See below.
SELECT
Country, Address, City
From
customers
LIMIT
5;
Try it on here:
DISTINCT keyword in SQL
To eliminate all duplicated data in the database, we can use
DISTINCT keyword. That keyword will grab only unique data from the database. The
basic syntax is:
SELECT
DISTINCT
Country FROM customers;
We can use more columns on this DISTINCT keyword. Just like:
SELECT
DISTINCT
Country, Address FROM customers;
Try it on here:
http://www.w3schools.com/sql/trysql.asp?filename=trysql_select_all
Basic Concepts of SQL
SELECT * FROM [Customers]
We can write a query to retrieve all informations from all columns of the database using an asterisk (*).
-
SELECT CustomerName FROM customers
SELECT CustomerName, Address FROM customers;
select CustomerName from customers;
SELECT CustomerName FROM customers;
sElEct CustomerName From customers;
-
Note: Multiple lines and non-useful white spaces are ignored in SQL. But its recommended to write your code without non-needed white lines or white spaces.
-
Try the code snaps above via this:
http://www.w3schools.com/sql/trysql.asp?filename=trysql_select_all
Kaydol:
Kayıtlar (Atom)