i've been on this for days and its givin me quite a headache...
I have a client model containing 开发者_C百科client details, i then have an address model containing different addresses for clients. I want to display the (CRUD) table under each client's details when i click on the details link in the (CRUD) table for the client. How do i go about doing that?
Either use a composite view model when displaying the client details or use AJAX to load the addresses when the client's details are displayed. I'd probably go with the former since the two are closely related and it seems like overkill to have a controller or action for just addresses (unless you would ever display them on their own without the other information).
public class ClientDetailsModel
{
public ClientDetails Details { get; set; }
public List<Address> Addresses { get; set; }
}
public ActionResult Details( int id )
{
var client = this.ClientRepository.GetByID( id );
var addresses = this.ClientRepository.GetAddressesForClient( client );
return View( new ClientDetailsModel { Details = client, Addresses = addresses } );
}
That's similar to what I would do, though it's more likely that I'd extract only the relevant properties and fill in a flat model class for each of the client/address parts rather than use the actual entity from the DB. AutoMapper can be a big help with this.
Once in the view you can abstract the address display into a partial view and render it for each address if you need to reuse the code.
... display client details...
@foreach (var address in Model.Addresses)
{
@Html.Partial( "_Address", address )
}
精彩评论