में डेटाबेस में व्यूमोडेल डेटा सहेजना मैं एएसपीनेट एमवीसी के लिए नया हूं और मेरे ड्रॉपडाउन को पॉप्युलेट करने के लिए व्यूबैग के बजाय व्यूमोडेल का उपयोग कर रहा हूं क्योंकि मैंने देखा है कि ज्यादातर लोग उनके खिलाफ अनुशंसा करते हैं। मेरे पास एक स्लिम यूआई है जो कैस्केडिंग ड्रॉपडाउन और ऑटोकंपलेट्स (यहां दिखाया नहीं गया है) लेकिन मुझे लगता है कि मेरा डेटा डेटाबेस में वापस सहेजा नहीं जा सकता है।ASP.NET MVC
मॉडल:
public partial class Car
{
public int CarID { get; set; }
public string CarName { get; set; }
public int ModelID { get; set; }
public int ManufacturerID { get; set; }
public int CarColorID { get; set; }
public Nullable<decimal> Price { get; set; }
public string Description { get; set; }
public virtual CarColor CarColor { get; set; }
public virtual Manufacturer Manufacturer { get; set; }
public virtual CarModel CarModel { get; set; }
}
public partial class CarColor
{
public CarColor()
{
this.Cars = new HashSet<Car>();
}
public int ColorID { get; set; }
public string ColorName { get; set; }
public virtual ICollection<Car> Cars { get; set; }
}
public partial class CarModel
{
public CarModel()
{
this.Cars = new HashSet<Car>();
}
public int CarModelID { get; set; }
public int ManufacturerID { get; set; }
public string CarModelName { get; set; }
public virtual ICollection<Car> Cars { get; set; }
public virtual Manufacturer Manufacturer { get; set; }
}
public partial class Manufacturer
{
public Manufacturer()
{
this.Cars = new HashSet<Car>();
this.Manufacturer1 = new HashSet<Manufacturer>();
this.CarModels = new HashSet<CarModel>();
}
public int ManufacturerID { get; set; }
public string ManufacturerName { get; set; }
public Nullable<int> ParentID { get; set; }
public virtual ICollection<Car> Cars { get; set; }
public virtual ICollection<Manufacturer> Manufacturer1 { get; set; }
public virtual Manufacturer Manufacturer2 { get; set; }
public virtual ICollection<CarModel> CarModels { get; set; }
}
ViewModel:
नियंत्रक:
public ActionResult Create()
{
var model = new AnotherTestViewModel();
using (new CarTestEntities())
{
model.CarModels = db.CarModels.ToList().Select(x => new SelectListItem
{
Value = x.CarModelID.ToString(),
Text = x.CarModelName
});
model.Manufacturers = db.Manufacturers.ToList().Select(x => new SelectListItem
{
Value = x.ManufacturerID.ToString(),
Text = x.ManufacturerName
});
model.CarColors = db.CarColors.ToList().Select(x => new SelectListItem
{
Value = x.ColorID.ToString(),
Text = x.ColorName
});
}
return View(model);
}
//
// POST: /AnotherTest/Create
[HttpPost]
public ActionResult Create(AnotherTestViewModel model)
{
if (ModelState.IsValid)
{
db.Entry(model).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Details", "AnotherTestViewModel", new { id = model.car.CarID });
}
return View();
}
मैं कुछ सिफारिशों को देखा Automapper उपयोग करने के लिए क्योंकि EntityState.Modified काम नहीं करेगा, लेकिन मैं मुझे यकीन नहीं है कि इसे कैसे कॉन्फ़िगर किया जाए क्योंकि नीचे दिए गए कोड का उपयोग नहीं किया गया था।
Mapper.CreateMap<AnotherTestViewModel, Car>();
Mapper.CreateMap<Car, AnotherTestViewModel>();
var newCar = Mapper.Map<AnotherTestViewModel, Car>(model);
कोई विचार?
बहुत बहुत धन्यवाद! यह वास्तव में मेरी मदद की ज़रूरत थी। – Jim
यह "अपडेटकर" फ़ंक्शन कहां रहता है? आप किसी प्रकार की "सेवा परत" में कहते हैं, क्या आप अधिक सटीक हो सकते हैं? धन्यवाद! – crunchy
सेवा वास्तव में यूआई के बारे में कुछ नहीं जानना चाहिए। तो आपके डोमेन/डेटा मॉडल में यह रूपांतरण आपके नियंत्रक में किया जाना चाहिए। फिर आप डोमेन/डेटा मॉडल में अपनी सेवा परत विधि में पास करते हैं जो अद्यतन करता है। – RiceRiceBaby