Using radio buttons by search in MVC4

Posted by Mandlaa under ASP.NET MVC on 10/7/2013 | Points: 10 | Views : 3929 | Status : [Member] | Replies : 2
Actually what i am doing here is,

I want search functionality in MVC4 with database
In my database
Tagno-----int

StockNo----Varchar in my database

I want to search data from database using Tagno or StokeNo
VIEW:
@using (Html.BeginForm("Sss","Hondaofconyers",FormMethod.Get))
{
<b>Search by</b>
@Html.RadioButton("searchBy", "TagNo", true)<text>Tagno</text>
@Html.RadioButton("searchBy", "StockNo", true)<text>StockNo</text>
<br />
@Html.TextBox("search")<input type="submit" value="Search"/>
}
CONTROLLER:
public ActionResult Sss(string searchBy,int search)
{
var styles = from s in db.ProductsHondaofConyers select s;
if (searchBy == "Gender")
return View(db.ProductsHondaofConyers.Where(x => x.TagNo == search).ToList());
else
return View(db.ProductsHondaofConyers.Where(x => x.StockNo==search).ToList());
}

Here i am using Radiobutton,Either search by Tagno or Stokeno
What is the mistake in the above my code ,
please help me,I am strocked this functionality




Responses

Posted by: Bandi on: 10/8/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Look into this once:
http://stackoverflow.com/questions/10518352/mvc4-two-radio-buttons-for-a-single-boolean-model-property
http://www.codeproject.com/Articles/611175/Radio-Buttons-for-List-Items-in-MVC-4-Problem-with

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Mandlaa, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Chakravarthi on: 1/22/2014 [Member] Starter | Points: 25

Up
0
Down
Hi Mandlaa,

Try this
VIEW:
 

@using (Html.BeginForm("Sss","Hondaofconyers",FormMethod.Get))
{
<b>Search by</b>
@Html.RadioButton("searchBy", "TagNo", true)
@Html.Label("Tagno")
@Html.RadioButton("searchBy", "StockNo")
@Html.Label("StockNo")
<br />
<input type="text" name="search"><input type="submit" value="Search"/>
}

CONTROLLER:
 

public ActionResult Sss(string searchBy,string search)
{
var styles = from s in db.ProductsHondaofConyers select s;

string searchType = Request["searchBy"];

if (!string.IsNullOrWhiteSpace(search))
{
if(searchType.Equals("TagNo"))
int q = int.Parse(search);
styles = styles.Where(s => s.TagNo == q).ToList();
else if(searchType.Equals("StockNo"))
styles = styles.Where(s => s.StockNo.Contains(search)).ToList();
}
return View(styles);
}

Regards

Chakravarthi

Mandlaa, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response