2010-06-17 10 views
5

szukałem modeli widoku dla mvc i szukam najlepszego sposobu na ich wykonanie. Czytałem mnóstwo różnych artykułów, ale żaden nie wydaje się być tak jasny jak "najlepszy sposób". Dotychczas Przykładem może mam model Klienta o następujących właściwościach:asp.net mvc viewmodels. Ile logiki (jeśli w ogóle) powinno zawierać

  • Imię
  • Nazwisko
  • Tytuł
  • Położenie

Jeżeli lokalizacja jest klucz obcy do lokalizacji tabela w bazie danych.

Chcę móc edytować tego klienta, ale tylko imię, nazwisko i lokalizację. Nie przeszkadza mi tytuł w edycji. Moim zdaniem będę musiał przekazać klienta i wybraną listę.

Teraz z tego, co przeczytałem, mam następujące opcje (prawdopodobnie jest ich o wiele więcej).

Moje pytanie jest w zasadzie najlepsze?

1)

Dodaj do listy select do ViewData["Location"] i wystarczy utworzyć silnie typami widoku klienta?

2)

Tworzenie widoku modelu, gdzie przechodzą mi klienta i wybierz listę (dostęp do danych odbywa się w kontrolerze):

public class ViewModelTest 
{ 
    public Customer Customer { get; set; } 
    public SelectList Locations { get; set; } 

    public ViewModelTest(Customer customer, SelectList locations) 
    { 
     Customer = customer; 
     Locations = locations; 
    } 
} 

3)

Tworzenie widoku modelu gdzie przekazuję klienta i listę lokalizacji i tworzę listę wyboru w modelu widoku.

public class ViewModelTest 
{ 
    public Customer Customer { get; set; } 
    public SelectList Locations { get; set; } 

    public ViewModelTest(Customer customer, List<Location> locations, string selectedLocation) 
    { 
     Customer = customer; 
     Locations = new SelectList(locations, "LocationID", "LocationName", selectedLocation); 
    } 
} 

4)

zdać klienta i repozytorium i zrobić dostęp do danych w modelu widoku.

public class ViewModelTest 
{ 
    public Customer Customer { get; set; } 
    public SelectList Locations { get; set; } 

    public ViewModelTest(Customer customer, IRepository repository, string selectedLocation) 
    { 
     Customer = customer; 
     Locations = new SelectList(repository.GetLocations(), "LocationID", "LocationName", selectedLocation); 
    } 
} 

5)

Tworzenie modelu widok z zaledwie właściwości muszę:

public class ViewModelTest 
{ 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 
    public SelectList Locations { get; set; } 

    public ViewModelTest(Customer customer, SelectList locations) 
    { 
     FirstName = customer.FirstName; 
     LastName = customer.LastName ; 
     Locations = locations; 
    } 
} 

6)

lub jakąś inną kombinację wyżej lub inny sposób.

Wszystkie opinie mile widziane.

Odpowiedz

6

Oto co mogę zasugerować: mają widok modelu, który odzwierciedla obszary silnie wpisany Widok:

public class SomeViewModel 
{ 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 
    public string Location { get; set; } 
    public IEnumerable<SelectListItem> PossibleLocations { get; set; } 
} 

I w akcji kontrolera zamieszkujących ten widok model:

public ActionResult Index() 
{ 
    var customer = Repository.GetCustomer(); 
    var locations = Repository.GetLocations(); 
    var viewModel = new SomeViewModel 
    { 
     FirstName = customer.FirstName, 
     LastName = customer.LastName, 
     Location = customer.Location, 
     PossibleLocations = new SelectList(locations, "LocationID", "LocationName", customer.Location); 
    }; 
    return View(viewModel); 
} 

[HttpPost] 
public ActionResult Index(SomeViewModel viewModel) 
{ 
    // TODO: Handle the form submission 
    return View(viewModel); 
} 

Oczywiście robi odwzorowanie między modelem i modelem widoku ręcznie, jak pokazano, mój przykład może stać się dość kłopotliwy iw tym przypadku polecam patrząc na AutoMapper.

2

ja mam ViewModel jak ten

public class SomeViewModel 
{ 
    public Customer Customer { get; set; } 
    public IEnumerable<Location> PossibleLocations { get; set; } 
} 

moim kontroler jak poniżej:

public ActionResult Index() 
{  
    var viewModel = new SomeViewModel 
    { 
     Customer = Repository.GetCustomer(), 
     PossibleLocations = Repository.GetLocations() 
    }; 
    return View(viewModel); 
} 

a następnie można uzyskać dostęp do wszystkiego w obiekcie Klienta w widoku jak ten:

Customer name - <%: Model.Customer.FirstName %> <%: Model.Customer.LastName %> 
Location - <%: Html.DropDownList("LocationID", new SelectList(Model.PossibleLocations as IEnumerable, "LocationID", "LocationName", Model.Location.LocationID))%> 
Powiązane problemy