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?