2010-06-17 13 views
5

हो सकता है कि मैं एमवीसी के लिए मॉडल देख रहा हूं और मैं उन्हें करने का सबसे अच्छा तरीका ढूंढ रहा हूं। मैंने अलग-अलग लेखों के भार पढ़े हैं लेकिन कोई भी "सर्वश्रेष्ठ तरीका" के रूप में स्पष्ट नहीं है। अब तक उदाहरण मैं निम्नलिखित गुणों के साथ एक ग्राहक मॉडल हो सकता है:asp.net mvc viewmodels। कितना तर्क (यदि कोई है) में

  • प्रथम नाम
  • अंतिम नाम
  • शीर्षक
  • स्थान

कहाँ स्थान एक स्थान के लिए एक विदेशी कुंजी है डेटाबेस में टेबल।

मैं इस ग्राहक को संपादित करने में सक्षम होना चाहता हूं लेकिन केवल पहला नाम, अंतिम नाम और स्थान। मुझे संपादन में शीर्षक के बारे में परेशान नहीं है। तो मेरे विचार में मुझे एक ग्राहक और एक चयनित सूची उत्तीर्ण करने की आवश्यकता होगी।

अब जो मैंने पढ़ा है उससे मेरे पास निम्नलिखित विकल्प हैं (शायद कई और हैं)।

तो मेरा प्रश्न मूल रूप से सबसे अच्छा है?

1)

ViewData["Location"] करने के लिए एक चयन सूची में जोड़ें और सिर्फ ग्राहक की प्रबलता से टाइप किये दृश्य बनाते हैं?

2)

एक दृश्य मॉडल जहां मैं एक ग्राहक का चयन करें और सूची से पारित बनाएँ (डेटा का उपयोग नियंत्रक में किया जाता है):

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

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

3)

एक दृश्य मॉडल बनाएं जहां मैं एक ग्राहक और स्थानों की सूची पास करता हूं और दृश्य मॉडल में चयन सूची बना देता हूं।

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)

एक ग्राहक और भंडार दर्रा और देखने मॉडल में डेटा का उपयोग करते हैं।

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)

सिर्फ गुण मैं जरूरत के साथ दृश्य मॉडल बनाएं:

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)

या ऊपर से या किसी अन्य तरह के कुछ अन्य संयोजन।

सभी राय स्वागत है।

उत्तर

6

यहाँ मैं क्या सुझाव दे सकते हैं है:

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); 
} 
बेशक

कर:

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

और अपने नियंत्रक कार्रवाई में इस दृश्य मॉडल पॉप्युलेट: जो दृढ़ता से टाइप किया दृश्य के क्षेत्रों को दर्शाता है एक दृश्य मॉडल है मॉडल और दृश्य मॉडल के बीच मैपिंग मैन्युअल रूप से दिखाया गया है कि मेरा उदाहरण काफी बोझिल हो सकता है और इस मामले में मैं आपको AutoMapper पर देखने की सलाह दूंगा।

2

मैं इस

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

इस तरह मेरे नियंत्रक के रूप में मेरी ViewModel होगा:

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

और उसके बाद आप इस तरह ध्यान में रखते हुए अपने ग्राहकों की वस्तु में सब कुछ का उपयोग कर सकते हैं:

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