कैसे बनाया गया मुझे इस पर कई लेख मिले लेकिन फिर भी मुझे नहीं पता कि यह वास्तव में कैसे करना है। मैं अपना ब्लॉग इंजन बनाने की कोशिश कर रहा हूं, मेरे पास आलेख बनाने के लिए व्यू है (मैं पहले ईएफ और कोड का उपयोग कर रहा हूं) और अब मुझे श्रेणी की संख्या भरनी होगी जिसमें लेख जोड़ना चाहिए, लेकिन मैं इसे ड्रॉपडाउन सूची में बदलना चाहता हूं श्रेणियाँ। मेरा मॉडल यह दिखता है:Asp.Net MVC3 - डायनेमिक ड्रॉपडाउनलिस्ट
public class Article
{
public int ArticleID { get; set; }
[Required]
public string Title { get; set; }
[Required]
public int CategoryID { get; set; }
public DateTime Date { get; set; }
[Required()]
[DataType(DataType.MultilineText)]
[AllowHtml]
public string Text { get; set; }
public virtual Category Category { get; set; }
public IEnumerable<SelectListItem> Categories { get; set; }
public virtual ICollection<Comment> Comments { get; set; }
}
public class Category
{
public int CategoryID { get; set; }
[Required]
public string Name { get; set; }
public virtual ICollection<Article> Articles { get; set; }
}
मुझे पता है कि मुझे Enum (या मुझे लगता है) का उपयोग करना चाहिए, लेकिन मुझे बिल्कुल यकीन नहीं है कि कैसे। मुझे नहीं पता कि मुझे जो ट्यूटोरियल मिला वह मेरे लिए सबसे अच्छा है।
संपादित करें:
अपने जवाब के लिए धन्यवाद, लेकिन मैं कुछ और ही पाया। मैं इस कोशिश कर रहा हूँ:
public class Article
{
[Key]
public int ArticleID { get; set; }
[Display(Name = "Title")]
[StringLength(30, MinimumLength = 5)]
[Required]
public string Title { get; set; }
public DateTime Date { get; set; }
public int CategoryID { get; set; }
[Required()]
[DataType(DataType.MultilineText)]
[AllowHtml]
public string Text { get; set; }
public Category Category { get; set; }
public virtual ICollection<Comment> Comments { get; set; }
public IEnumerable<Category> Categories { get; set; }
}
public class Category
{
[Key]
public int CategoryId { get; set; }
[Required]
public string CategoryName { get; set; }
public virtual ICollection<Article> Articles { get; set; }
}
यह लेख बनाने के लिए मेरी नियंत्रक है:
यह मेरा मॉडल है
public ActionResult Vytvorit()
{
IEnumerable<Category> categories = GetCaregories();
var view = View(new Article() { Categories = categories });
view.TempData.Add("Action", "Create");
return view;
}
private static IEnumerable<Category> GetCaregories()
{
IEnumerable<Category> categories;
using (BlogDBContext context = new BlogDBContext())
{
categories = (from one in context.Categories
orderby one.CategoryName
select one).ToList();
}
return categories;
}
private Category GetCategory(int categoryID)
{
return db.Categories.Find(categoryID);
}
//
// POST: /Clanky/Vytvorit
[HttpPost]
public ActionResult Vytvorit(Article newArticle)
{
try
{
if (newArticle.CategoryID > 0)
{
newArticle.Category = GetCategory(newArticle.CategoryID);
}
if (TryValidateModel(newArticle))
{
db.Articles.Add(newArticle);
db.SaveChanges();
return RedirectToAction("Index");
}
else
{
newArticle.Categories = GetCaregories();
var view = View(newArticle);
view.TempData.Add("Action", "Create");
return view;
}
}
catch
{
return View();
}
}
और यह मेरे विचार का हिस्सा है:
@Html.DropDownListFor(model => model.CategoryID, new SelectList(Model.Categories,"CategoryID","CategoryName"))
@Html.ValidationMessageFor(model => model.CategoryID)
मुझे NullReferenceExeption के साथ समस्या है लेकिन मुझे नहीं पता क्यों। क्या मैं इसे इस तरह से कर सकता हूं? यह मेरे लिए बहुत आसान लग रहा है।
बस jQuery UI के साथ बॉक्स से बाहर निकलने वाले व्यक्ति का उपयोग करें? – CarneyCode