0

I have a question, can this be done using ASP.NET MVC (Razor, and MapRoute)?

Example

  • Category/ - calls controller Category and Index function
  • Category/{State_name} - calls controller Category and Cities(State_id) function, returns all cities inside that state.

So URL is displaying state name , but Cities function receive state id?

2 Answers 2

2

Yes u can, try

public class CategoryController : Controller {

    // GET: /Category/
    // OR
    // GET: /Category/State_name
    public ActionResult Index(string State_name) {
        if (!string.IsNullOrWhiteSpace(State_name)) {
            int? State_id = FindStateIdFromStateName(State_name); // retrive state_id from datastore where state_name == State_name
            if (State_id.HasValue) { // means the currect state_name passed to action
                var model = FindStateById(State_id.Value);
                return View("Cities", model); // will renders Cities.cshtml with specified model
            } else {
                // means the specified state_name not found! u can do something else, or allow continue bellow lines
            }
        }
        return View(); // will render normal Index.cshtml
    }

}

Let me know if you have any questions or need clarifications on any part.

UPDATE

I have one issue with the way! You get the ID from db with State_name, then get the model from db by State_name! Why not retrieve model from db by State_name at the first time? look:

public class CategoryController : Controller {

    // GET: /Category/
    // OR
    // GET: /Category/State_name
    public ActionResult Index(string State_name) {
        if (!string.IsNullOrWhiteSpace(State_name)) {
            var model = FindStateByName(State_name);
            if(model != null)
                return View("Cities", model);
            else
                // means the specified state_name not found! u can do something else, or allow continue bellow lines
        }
        return View(); // will render normal Index.cshtml
    }

}

and if you are on EF, then you can create this method:

public State FindStateByName(state_name){
    using(var context = new YourEntityContext()){
         return context.States.SingleOrDefault(s => s.Name.ToLower() == state_name.ToLower());
    }
}

Why not using this way?

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for your help. I guess there isn`t a way to do this without going to database to retrieve id?
Do you mean that you want to retrieve the ID from client without putting it in query-string?
I have a issue with that! You first get the ID from db with name, then get the model with ID! Why not get the model with name at first time?
Yes, i meant exactly like that. I prefer retrieving information by ID :) I`ll use code from your first post. Thank you.
2

This should do it:

routes.MapRoute("Category_byState", "Category/{State_id}", new { controller = "Category", action = "Cities" });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.