ebb
ebb

Reputation: 9377

ASP.NET MVC - Multiple views with same name?

This question is mostly related to my previous question. I'm having an Edit view for my Product site to list the products with edit/delete link attached.. But how can I do:

/products/edit = show list with edit/delete links.

/products/edit/{productId} = show edit model (textboxes etc) for the specific product.

Upvotes: 0

Views: 792

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038890

You could always do this:

public ActionResult Edit(int? productId)
{
    if (productId != null)
    {
        return View("ViewWithTextBoxes.aspx");
    }
    return View("ViewWithEditDeleteLinks.aspx");
}

This being said, the case of showing edit and delete links doesn't seem like editing so I would recommend you using different action names. It seems more RESTful to me like this:

/products/index
/products/edit/{productId}

In this case you are having different actions and views for each case:

public ActionResult Index()
{
    var products = _repository.GetProducts();
    return View(products);
}

public ActionResult Edit(int productId)
{
    var product = _repository.GetProduct(productId);
    return View(product);
}

Upvotes: 2

Related Questions