views:

7

answers:

0

Hi I have a what I consider a standard example. In my asp.net MVC 2 solution, I have an Index View for my Products Controller. So when a user browse to the products url, the index view will get displayed and show a list products in a listbox. The View code look like this:

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<% using (Html.BeginForm("HandleSubmit", "Products"))
    {%>


    <h2>ProductsIndex</h2>

    <%: Html.ListBoxFor(m=>m.SelectedProduct,Model.ProductsSelectList,new { id = "productList" } )%>

    <p>
        <%: Html.ActionLink("Create New", "Create", new { id = "createProduct" }) %>
        <%: Html.ActionLink("Details", "Details", new { id = "productDetails" })%>
        <%: Html.ActionLink("Edit", "Edit", new { id = "editProduct" })%>
        <%: Html.ActionLink("Delete", "Delete", new { id = "deleteProducts" })%>
    </p>

    <p>
        <input type="submit" name="productAction" value="CreateProduct"  />
        <input type="submit" name="productAction" value="ProductDetails"  />
        <input type="submit" name="productAction" value="EditProduct" />
        <input type="submit" name="productAction" value="DeleteProduct"  />
    </p>
<%} %>   
</asp:Content>

What I would like to do now is to redirect to Edit, detail, Create views based on the selection in the listbox. And at the same time have nice urls displayed in the status bar when hovering over the links or buttons.

Several proposals exists to handle this problem.

Some suggest javascript/JQuery

http://stackoverflow.com/questions/2427926/how-to-go-other-page-on-dropdown-selection-using-asp-net-mvc

Others suggest using multiple form tags

Others suggest having logic retrieving the button name or value and let the Action redirect based on that information

finally some suggests making an attrbute/actionfilter like this

//
// GET: /Products/Edit/5
[MultiButton(MatchFormKey = "productAction", MatchFormValue = "EditProduct")]
public ActionResult Edit(WareHouse model)
{
    int id = int.Parse(model.SelectedProduct);
    return View("Edit",model.Products[id]);
}

But all of these have down sides.

What if Javascript is disabled. This might seem an academic problem, since MVC and JQuery seems to go hand in hand. So alot of stuff will not be psossible without javascript.

The other solutions complicates things if You want to use localization. And finally they give bad looking urls. in my case something containing "HandleSubmit" because of the

 <% using (Html.BeginForm("HandleSubmit", "Products"))
        {%>

May be this is why all examples put edit, details, delete links for each item in at table or list. But From a UI point of view i don't like beeing restricted to that solution.

Could this possibly be solved with a custom route?