3

I have a dynamic url processor

public ActionResult DynamicUrl(string slug = null)

this method works through the slug (btw, what does slug stand for?) and works out if the slug is displaying a product or performing a product search.

As part of the product search, I have a page=1 querystring param.

E.g. /Womens/Dresses?page=2

Usually I would do this in a normal product search action which binds the page querystring to the ProductSearch model.

public ActionResult Results(ProductSearchModel  searchModel)

How can I bind the querstring during the action? For example

public ActionResult DynamicUrl(string slug = null)
{
    ProductSearchModel psm = new ProductSearchModel();

    //Auto bind psm here.
    // E.g. Controller.BindModel(psm);
}

Hope I am not way off course on this.

Valamas
  • 24,169
  • 25
  • 107
  • 177
  • 2
    A slug is a human readable, friendly part of a url usually appended to the end of the url. If you enter just `http://stackoverflow.com/questions/30386176` in the address bar, notice how it appends `/how-to-bind-model-during-an-action` to the end of it when the page is rendered –  May 21 '15 at 23:47
  • http://stackoverflow.com/questions/6055415/adding-id-and-title-to-url-slugs-in-asp-net-mvc Is this what you are looking for? – Medo May 21 '15 at 23:50
  • If you have a query string `?page=2`, then the method should have a matching parameter - public ActionResult DynamicUrl(string slug, int page)` (no need to have `string slug = null` since `string` is a reference type) –  May 21 '15 at 23:50
  • **None of the comments above are on the same subject I am asking about.** – Valamas May 22 '15 at 01:19
  • Why you dont want to use Request.QueryString ? – Marcin Zablocki May 22 '15 at 01:26
  • My first comment was a response to your query _"btw, what does slug mean?"_ and my second comment shows how to add a parameter so you can use `Model.Page = page` instead of _"resorting to Model.Page = Request.Querystring["Page"]"_. What is it that you really need to know? –  May 22 '15 at 02:26
  • I meant, what does "slug" stand for. Is it an acronym for something? As for the second comment, i want to bind the models to any querystring. So i do not want to have to list them all as params in the action, then assign them manually to the model. I have updated my last code example and hope that clears up that confusion. – Valamas May 22 '15 at 20:38
  • @Valamas-AUS - Slug isn't an acronym, it's just a name adopted by web devs to parts of Urls. I'd generally classify them as configurable URL parts so the slug would be the part the user can control. – webnoob May 22 '15 at 23:01

1 Answers1

2

Do you mean:

UpdateModel(psm);

This will bind the current form collection to the model specified.

You can also use:

TryUpdateModel(psm);

This version won't throw an exception if something fails and returns true or false.

webnoob
  • 15,747
  • 13
  • 83
  • 165