tags:

views:

57

answers:

2

I'm trying to set up a page where I display a list of items and the details of the selected item. I have it working but wonder whether I have followed the correct approach. I'll use customers as an example

I have set the aspx page to inherit from an IEnumerable of Customers. This seems to be the standard approach to display the list of items. For the Details I have added a Customer user control which inherits from customer.

I think i'm on the right track so far but I was a bit confused as to where I should store the id of the customer whose details I intend to display. I wanted to make the id optional in the controller action so that the page could be hit using "/customers" or "customers/1" so I made the arg optional and stored the id in the ViewData like this:

public ActionResult Customers(string id = "0")
    {
        Models.DBContext db = new Models.DBContext();
        var cList = db.Customers.OrderByDescending(c => c.CustomerNumber);
        if (id == "0")
        {
            ViewData["CustomerNumber"] = cList.First().CustomerNumber.ToString();
        }
        else
        {
            ViewData["CustomerNumber"] = id;
        }
        return View("Customers", cList);
    }

I then rendered the User control using RenderPartial in the front end:

<%var CustomerList = from x in Model
                        where x.CustomerNumber == Convert.ToInt32(ViewData["CustomerNumber"])
                 select x;
         Customer c = (Customer)CustomerList.First(); %>
<% Html.RenderPartial("Customer",c); %>

Then I just have an actionLink on each listed item: <%: Html.ActionLink("Select", "Customers", new { id = item.CustomerNumber })%>

It all seems to work but as MVC is new to me I would just be interested in others thoughts on whether this is a good approach?

+1  A: 

In this case I would take the following approach. Display a list of customer's, be it a listbox or drop down list. Let's assume it's a drop down list, probably not very user friendly

You would have the text as the customer name and then the value as the customer id

<select title="Customers">
    <option label="Pieter" value="001"></option>
</select>

and using JQuery's $.getJSON you could load the new data via a call to an asp.net MVC action. Note that your Action should return JsonResult

PieterG
So the thinking behind this would be to make the url more semantic? ie. so you could have /customers and /customer/{id}. How would I avoid "opening" up the /customer/{id} url though? as I only really want this displayed from the view that is rendered using /customers
fizzer
+2  A: 

In regards to proper MVC and separations of concerns, you shouldn't be calling LINQ queries in your view. To get around that, change your controller action code from what you have to this:

if (id == "0") 
{ 
    ViewData["CustomerDetails"] = cList.First();
} 
else 
{ 
    ViewData["CustomerDetails"] = From x in db.customers where x.id = cInt(id); 
} 

then your partial

<% Html.RenderPartial("Customer",ViewData["CustomerDetails"]); %> 

Are you showing the customer information on the same screen that you have your list of customers and not a separate view?

Tommy
Many thanks. Yes that makes sense to me now
fizzer
Yes I am showing everything in the same view
fizzer