I have the music store demo but did not see a 'search' in there. I can look at the other.
The point Andrei was making wasn't to go look for a search example, but to understand the basics of mvc in order to build what you need.
For example, page 50 of the music store tutorial includes code the Browse action method. That takes a genre name and looks up all albums by that genre.
That is very similar to what you need, but in your case you will be querying a company name and not a genre. Also in your case the comparison will be .StartsWith instead of ==. Something like
public ActionResult CompanyLookUp(string company)
{
var companies= db.Companies.Where(c=>c.companyName.StartsWith(company));
return View(companies.ToList());
}
As Andrei pointed, do the tutorials. Your employer might block certain downloads,but If you can get to this forum, you can get to these tutorials since it's on this site (note music store is on the bottom). http://www.asp.net/mvc/tutorials
Do you build a HttpPost version of the same controller action?
I guess the difference I was looking for between 'browse' on the music store sample and a 'search' is that the serach might involve an HttpPost controller action that takes a model or a form field collection as input.
So if I have a controller action of Search()
should I take the route of custom model for search and have an HttpPost with the model as input, or just a simple form field collection?
public ActionResult Search()
...blah
[HttpPost]
public ActionResults Search(someformfieldcollection)
Don't use HttpPost for searching! If you do this, users will not be able to refresh the page. My rule of thumb is to only use HttpPost when data is being updated, added, or deleted.
modify your action to filter the results and return them to the view.
Your action will look like this:
public ActionResult Index(string filter)
{
var model = repository.GetEmployees();
if (!string.IsNullOrEmpty(filter))
{
model = (from x in model
where x.FullName.ToLower().Contains(filter.ToLower())
orderby x.FullName
select x).ToList();
}
return View(model);
}
Your controller will then need a form (on the Index page in my example) that looks like this:
There you would be getting into model binding which becomes a bit more complex, but still not overly difficult. You would create a class containing the different fields you want to filter like so:
public class FilterClass
{
[Required] //use data annotations to validate properties here
public string NameFilter { get; set; }
[Required]
public string CityFilter { get; set; }
[Required]
public string StateFilter { get; set; }
}
Then your action would take the FilterClass as it's parameter
public ActionResult Index(FilterClass filterClass)
{
var employees = repository.GetEmployees();
// do your filtering here, access properties like so:
// filterClass.NameFilter
// filterClass.CityFilter
// filterClass.StateFilter
return (employees);
}
<div>Hi Agent,</div> <div>In addtion to @Ryanw51, I suggest that you use Ajax.BeginForm to update the data dispaly section only. Something like this:
@using (Ajax.BeginForm("Index", "Home", new AjaxOptions{UpdateTargetId = "data-grid", HttpMethod="Post"}))
{
@Html.TextBox("NameFilter")
@Html.TextBox("CityFilter")
@Html.TextBox("StateFilter")
<input type="submit" value="Search" />
}
<div id="data-grid">
@Html.Partial("DisplayResult", Model-object)
</div>
@*To use Ajax to update specific block, the follow script need to be add*@
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"></script>
agent_smith
Contributor
2030 Points
572 Posts
How to build and MVC3 search form and results
Apr 11, 2011 07:25 PM|LINK
I feel like a complete idiot for having to ask this.
I need to build a search form and a search results page for a simple search.
1) Type in a partial company name (maybe a couple of other fields)
2) Click search.
3) Get search results
Easy in ASp.NET of course.
Newbie with the MVC3/Razor.
Thanks a bunch.
AGENT_SMITH
ignatandrei
All-Star
134971 Points
21637 Posts
Moderator
MVP
Re: How to build and MVC3 search form and results
Apr 11, 2011 07:52 PM|LINK
Did you follow MVC MusicStore/NerdDinner tutorials from http://www.asp.net/mvc/ ?
agent_smith
Contributor
2030 Points
572 Posts
Re: How to build and MVC3 search form and results
Apr 11, 2011 08:12 PM|LINK
I have the music store demo but did not see a 'search' in there. I can look at the other.
**
My employer blocks downloads from that site.
I do have a copy of the music store and can run that in VS2010, but there is no 'search' tool in there.
Thanks.
AGENT_SMITH
CodeHobo
All-Star
18647 Points
2647 Posts
Re: How to build and MVC3 search form and results
Apr 11, 2011 10:38 PM|LINK
The point Andrei was making wasn't to go look for a search example, but to understand the basics of mvc in order to build what you need.
For example, page 50 of the music store tutorial includes code the Browse action method. That takes a genre name and looks up all albums by that genre.
That is very similar to what you need, but in your case you will be querying a company name and not a genre. Also in your case the comparison will be .StartsWith instead of ==. Something like
public ActionResult CompanyLookUp(string company) { var companies= db.Companies.Where(c=>c.companyName.StartsWith(company)); return View(companies.ToList()); }As Andrei pointed, do the tutorials. Your employer might block certain downloads,but If you can get to this forum, you can get to these tutorials since it's on this site (note music store is on the bottom).
http://www.asp.net/mvc/tutorials
Blog | Twitter : @Hattan
agent_smith
Contributor
2030 Points
572 Posts
Re: How to build and MVC3 search form and results
Apr 12, 2011 12:39 PM|LINK
I have a full working MVC3 site. I was just looking for a 'good' search example from you kind folks out there.
I do not want to build something 'wrong' versus following a path that others have already blazed.
Thanks for your input.
AGENT_SMITH
agent_smith
Contributor
2030 Points
572 Posts
Re: How to build and MVC3 search form and results
Apr 12, 2011 12:43 PM|LINK
Do you build a HttpPost version of the same controller action?
I guess the difference I was looking for between 'browse' on the music store sample and a 'search' is that the serach might involve an HttpPost controller action that takes a model or a form field collection as input.
So if I have a controller action of Search()
should I take the route of custom model for search and have an HttpPost with the model as input, or just a simple form field collection?
public ActionResult Search()
...blah
[HttpPost]
public ActionResults Search(someformfieldcollection)
... results page
Is that what I should be attempting?
Thanks.
AGENT_SMITH
ryanw51
Contributor
2363 Points
511 Posts
Re: How to build and MVC3 search form and results
Apr 12, 2011 01:22 PM|LINK
Don't use HttpPost for searching! If you do this, users will not be able to refresh the page. My rule of thumb is to only use HttpPost when data is being updated, added, or deleted.
modify your action to filter the results and return them to the view.
Your action will look like this:
public ActionResult Index(string filter) { var model = repository.GetEmployees(); if (!string.IsNullOrEmpty(filter)) { model = (from x in model where x.FullName.ToLower().Contains(filter.ToLower()) orderby x.FullName select x).ToList(); } return View(model); }Your controller will then need a form (on the Index page in my example) that looks like this:
@using (Html.BeginForm("Index", "Home", FormMethod.Get)) { @Html.TextBox("filter") <input type="submit" value="Search" /> }agent_smith
Contributor
2030 Points
572 Posts
Re: How to build and MVC3 search form and results
Apr 12, 2011 05:30 PM|LINK
Thanks. That is great info.
How would you implement more form fields for an 'advanced search'?
How could I implement validation so the client had to complete some of the search fields?
AGENT_SMITH
ryanw51
Contributor
2363 Points
511 Posts
Re: How to build and MVC3 search form and results
Apr 12, 2011 09:02 PM|LINK
There you would be getting into model binding which becomes a bit more complex, but still not overly difficult. You would create a class containing the different fields you want to filter like so:
public class FilterClass { [Required] //use data annotations to validate properties here public string NameFilter { get; set; } [Required] public string CityFilter { get; set; } [Required] public string StateFilter { get; set; } }Then your action would take the FilterClass as it's parameter
public ActionResult Index(FilterClass filterClass) { var employees = repository.GetEmployees(); // do your filtering here, access properties like so: // filterClass.NameFilter // filterClass.CityFilter // filterClass.StateFilter return (employees); }in your view:
@using (Html.BeginForm("Index", "Home", FormMethod.Get)) { @Html.TextBox("NameFilter") @Html.TextBox("CityFilter") @Html.TextBox("StateFilter") <input type="submit" value="Search" /> }Now, it's up to you to design the GUI and the filter controller. I can't tell you what to do there as I don't know what your specific needs are.
Forest Cheng...
Star
8370 Points
819 Posts
Re: How to build and MVC3 search form and results
Apr 13, 2011 01:49 AM|LINK
@using (Ajax.BeginForm("Index", "Home", new AjaxOptions{UpdateTargetId = "data-grid", HttpMethod="Post"})) { @Html.TextBox("NameFilter") @Html.TextBox("CityFilter") @Html.TextBox("StateFilter") <input type="submit" value="Search" /> } <div id="data-grid"> @Html.Partial("DisplayResult", Model-object) </div> @*To use Ajax to update specific block, the follow script need to be add*@ <script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"></script></div> <div>What's more, there is a search demo wiht ASP.NET MVC:http://dotnetunplugged.com/2009/07/25/asp-net-mvc-grid-%e2%80%93-part-7-%e2%80%93-reusable-control/</div>
<div>Hope this helpful,Forest Cheng</div>
If you have any feedback about my replies,please contact msdnmg@microsoft.com.
Microsoft One Code Framework