How to search with parameters in MVC?
I need to be able to provide a page for the end user where thay can search in the following :
- Search work
- Category
- Price开发者_如何学Python
- AdType
- Location
and so on.
Its important that the user can copy the url and then use it later(and get the same settings). Its also important to be able to save these settings on the user in the database.
So far I have created a ModelView class that contains the parameters but I am not sure that this is the right way to go? Maby I should pass all inte the URL.
If so, how do I accomplish this? Is there any samples I could look at?
BestRegards
The least problematic way is to pass the parameters in the queryString.
mysite.com/search/?c=1&p=1.00&at=blah&location=75081&.....
There's nothing non-MVC about this approach. In fact, you may see ActionLinks generated using QueryString parameters when passing parameters that don't correspond to model fields.
You could create a route like
Search/{Category}/{Price}/{AdType}/{Location}/...
but, in my opinion that's the wrong way to go. I assume, possibly incorrectly, that using a route would make the URL more search engine-friendly, but that's only an issue if you want different combinations of search urls to be discoverable by an external search engine, which really doesn't make as much sense as a sitemap.
Update:
If you declare your action like so:
public ActionResult Search(string category, float price, string adType, float location)
{
/// do whatever you want in here
}
Then your queryString parameters will be mapped to the action method parameters.
All you'll need to do is set your form "method" to GET,
<form method="GET" ....>
to get your fields passed as query string parameters instead of form POST data.
Sorry but the comment field is to small to use.
The problem is that my webmethod looks like this :
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult List(AdList adList)
{
try
{
return View(adList);
}
catch (Exception ex)
{
throw ex;
}
}
The AdList looks like this :
public class AdList
{
public AdList()
{
this.ModelViewAd = new List<ModelViewAdList>();
}
public List<ModelViewAdList> ModelViewAd { get; set; }
public SeartchParameters ModelViewSeartchParameters { get; set; }
}
And this is how the members looks like :
public class SeartchParameters
{
public string SeartchString { get; set; }
public int CategoryId { get; set; }
public int MunicipalityId { get; set; }
public int CountryId { get; set; }
}
public class ModelViewAdList
{
[DisplayName("Titel")]
public string Titel { get; set; }
[DisplayName("Köp ny pris")]
public Decimal BuyNowPrice { get; set; }
[DisplayName("Reservations pris")]
public Decimal ReservationPrice { get; set; }
[Required(ErrorMessage = "Annons text saknas")]
[DisplayName("Annons text")]
public string Description { get; set; }
[DisplayName("Slutdatum")]
public DateTime? EndDate { get; set; }
[DisplayName("Frakt kostnad")]
public Decimal Shipping { get; set; }
[DisplayName("Produktens Skick")]
public ProductStatus StateOfProduct { get; set; }
[DisplayName("Start pris")]
public Decimal StartingPrice { get; set; }
[DisplayName("Annonstyp")]
public RadioButtonListViewModel<AdType> TypeOfAd { get; set; }
}
If I remove the AdList as inparameter and then ads the seartch parametes instead it will probably not work becourse MVC is trying to send back alot of objects?
So how do I solve this in combination with David´s example?
精彩评论