तो मेरा कोड पहले काम कर रहा था। मुझे नहीं पता कि मैंने ऐसा करने के लिए क्या किया और मैं इसे ठीक नहीं कर सकता। मैंने लोगों को मॉडलस्टेट को रीसेट करने के लिए कहा है। (ModelState.Clear();) लेकिन इससे मदद नहीं मिली। इसके अलावा, यह मदद नहीं करता है कि मैं अभी भी एमवीसी के लिए बिल्कुल नया हूँ। किसी भी सहायता की सराहना की जाएगी। धन्यवाद।व्यूडाटा आइटम जिसमें कुंजी 'Category आईडी' है, 'System.Int32' प्रकार का है, लेकिन 'IEnumerable <SelectListItem>' प्रकार का होना चाहिए?
नियंत्रक:
public ActionResult Create()
{
ActiveDirectoryModel adm = new ActiveDirectoryModel();
ViewBag.notifyto = adm.FetchContacts();
var model = Populate();
return View(model);
}
[HttpPost]
public ActionResult Create(CreateViewModel model)
{
if (ModelState.IsValid)
{
model.leaf.Date = DateTime.Now.Date;
model.leaf.Category = model.CategoryId;
model.leaf.SubCategory = model.SubCategoryId;
model.leaf.AssignedTo = model.AssignedToId;
model.leaf.CoAssignedTo = model.CoAssignedToId;
model.leaf.Status = model.StatusId;
model.leaf.Priority = model.PriorityId;
//model.lead.Parent = model.ParentID;
db.LeafItems.AddObject(model.leaf);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(model);
}
public CreateViewModel Populate()
{
ActiveDirectoryModel adm = new ActiveDirectoryModel();
var model = new CreateViewModel
{
AssignedToItems = adm.FetchContacts(),
CoAssignedToItems = adm.FetchContacts(),
NotifyToItems = adm.FetchContacts(),
CategoryItems =
from c in new IntraEntities().CategoryItems.ToList()
select new SelectListItem
{
Text = c.Name,
Value = c.ID.ToString()
},
SubCategoryItems =
from sc in new IntraEntities().SubCategoryItems.ToList()
select new SelectListItem
{
Text = sc.Name,
Value = sc.ID.ToString()
},
StatusItems =
from s in new IntraEntities().StatusItems.ToList()
where s.IsPriority == false
select new SelectListItem
{
Text = s.Name,
Value = s.ID.ToString()
},
PriorityItems =
from p in new IntraEntities().StatusItems.ToList()
where p.IsPriority == true
select new SelectListItem
{
Text = p.Name,
Value = p.ID.ToString()
}
};
return model;
}
दृश्य:
<div class="createTopInner">
<div class="editor-label">
@Html.LabelFor(model => model.leaf.Category)
</div>
<div class="editor-field">
@Html.DropDownListFor(model => model.CategoryId, Model.CategoryItems, "")
@Html.ValidationMessageFor(model => model.leaf.Category)
</div>
</div>
मॉडल:
public int CategoryId { get; set; }
public IEnumerable<SelectListItem> CategoryItems { get; set; }
मॉडलस्टेट.इस्वालिड = झूठी अगर मैंने अपनी जनसंख्या विधि को चलाकर समस्या को ठीक किया। एक बार 8 घंटे गुजरने के बाद मैं इसे एक वास्तविक उत्तर के रूप में पोस्ट कर सकता हूं। मैं नहीं चाहता कि कोई भी तब तक अपना समय बर्बाद कर रहा हो। – Ber53rker