【发布时间】:2014-05-05 14:12:57
【问题描述】:
我无法在 ASP.NET MVC5 中创建正确的创建/编辑视图。我有两个型号Dog 和Human。一只狗属于一个Human。我正在尝试在create 和edit 视图中为Dog 创建一个下拉列表,这将允许我为特定的Dog 按名称选择Human。这是我的模型:
人类:
public class Human
{
public int ID { get; set; }
public string Name { get; set; }
}
狗:
public class Dog
{
public int ID { get; set; }
public string Name { get; set; }
public Human Human { get; set; }
}
我的创建操作:
// GET: /Dog/Create
public ActionResult Create()
{
ViewBag.HumanSelection = db.Humen.Select(h => new SelectListItem
{
Value = h.ID.ToString(),
Text = h.Name
});
return View();
}
这是我观点的相关部分:
<div class="form-group">
@Html.LabelFor(model => model.Human.Name, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.Human, ViewBag.HumanSelection);
</div>
</div>
运行时出现以下错误:
Compiler Error Message: CS1973: 'System.Web.Mvc.HtmlHelper<Test.Models.Dog>' has no applicable method named 'DropDownListFor' but appears to have an extension method by that name. Extension methods cannot be dynamically dispatched. Consider casting the dynamic arguments or calling the extension method without the extension method syntax.
我是 C# 和实体框架的新手。我究竟做错了什么?有没有办法在不手动查询数据库的情况下做到这一点?像 Rails 中的收集表单助手之类的东西? 我学习了一堆旧的或太复杂的教程。
【问题讨论】:
-
我认为你的问题并不清楚问题是什么。您是否收到错误消息?
-
以下链接可能对您有所帮助。这对我有用
ilyasmamunbd.blogspot.com/2014/03/…
标签: c# asp.net asp.net-mvc