每当我添加一个新的应用程序它创建一个新的AppCategory。我很认真地拧紧这在某种程度上ASP.NET EditorTemplate DropdownList
代码第一个实体框架对象
public class AppCategory
{
public int ID { get; set; }
public string Name { get; set; }
public ICollection<App> apps { get; set; }
}
public class App
{
public int ID { get; set; }
public string Name { get; set; }
public AppCategory Category { get; set; }
}
编辑模板(我很想只是做只有一个外键EditorTemplate)
@inherits System.Web.Mvc.WebViewPage
@Html.DropDownList("Category", LIG2010RedesignMVC3.Models.Repo.GetAppCategoriesSelect())
,当然还有库
public static IEnumerable<SelectListItem> GetAppCategoriesSelect()
{
return (from p in GetAppCategories()
select new SelectListItem
{
Text = p.Name,
Value = p.ID.ToString(),
});
}
public static ICollection<AppCategory> GetAppCategories()
{
var context = new LIGDataContext();
return context.AppCategories.ToList();
}
每当我添加一个新的应用程序它创建一个新的AppC ategory我很认真地拧紧这在某种程度上
增加更多的调试信息
@inherits System.Web.Mvc.WebViewPage
@Html.DropDownList("", LIG2010RedesignMVC3.Models.Repo.GetAppCategoriesSelect())
使我对后
Parameters application/x-www-form-urlencoded
Category 1
Name 8
验证错误值验证消息 '1'是无效的。
这很有意义,因为类别应该是一个不是整数的对象。
控制器守则要求 很肯定,因为它从MVCScaffold
[HttpPost]
public ActionResult Create(App d)
{
if (ModelState.IsValid)
{
context.Apps.Add(d);
context.SaveChanges();
return RedirectToAction("Index");
}
return View();
}
我不知道你的问题是什么,或者你是怎么对文章的给予好评。你有没有在调试器中通过程序来缩小发生问题的地方? – 2010-11-15 20:48:54
从控制器[HttpPost] public ActionResult创建(应用d)我得到d.Category为空(这就是为什么它创建一个新的),但我不知道为什么我得到d.Category为空 – MarkKGreenway 2010-11-15 21:26:30
请发布您的控制器代码。我相当确定问题在那里。 – jfar 2010-11-16 17:07:51