views:

65

answers:

2

Hi,

I need to be able to provide a page for the end user where thay can search in the following :

  • Search work
  • Category
  • Price
  • 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

+2  A: 

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.

David Lively
So what you are saying is that I whould construct somekind of queryString on submit that takes all my parameters in to account. Then in on the server side I will extraxt the quaryString and validate it against injection attacs. When this is valid I am able to get the right data back to the client?Should I use a queryString and only include the choosen parameters or should I go with the route?And how do I construct the querystring as easy as possible?
SnowJim
Setting your form method to "GET" will cause your form fields to be passed as queryString parameters instead of POST data. You'll need to validate your data on the server in your action method. If you're searching a database, use a stored procedure to perform the search, and a good library like Enterprise Library to call the stored procedure as this will help protect against injection. See the updat above.
David Lively
Thanks, but does this mean that I will have to generate the querystring manually on the client on submit in somekind of javascript function? Do you maby got a example on how to generate this querystring?
SnowJim
No, setting the form's method type to "GET" will cause the querystring to be generated when you hit the button to submit the form.
David Lively
Grate! This sounds like a good solution. Thanks
SnowJim
This comment field is to small to post code in so I hade to make a ny post, see bellow.
SnowJim
A: 

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?

SnowJim