2016-08-16 48 views
3

我在使用EF创建新的数据库对象时遇到了问题。它有一个外键,在使用预设视图时,可以使用下拉菜单进行设置。MVC设置外键没有下拉

@Html.DropDownList("ParentID", null, htmlAttributes: new { @class = "form-control" }) 

的问题是,我不希望编辑该值,则需要在控制器中进行设置,但我不能访问的价值。

public ActionResult Create() 
{ 
    //I don't want a select list ParentID needs to be passed from the parent. 
    ViewBag.ParentID = new SelectList(db.Parents, "ParentID", "ParentName"); 
    return View(); 
} 

[HttpPost] 
[ValidateAntiForgeryToken] 
public ActionResult Create([Bind(Include = "ChildID,ParentID,ChildName")] Child child) 
{ 
    if (ModelState.IsValid) 
    { 
     //How do I set child.ParentID without a select list??? 
     db.Child.Add(child); 
     db.SaveChanges(); 
     return RedirectToAction("Index"); 
    } 

    ViewBag.ParentID = new SelectList(db.Parents, "ParentID", "ParentName", child.ParentID); //I don't need this 
    return View(child); 
} 

这是通过什么方式实现的?

+0

的问题是,你在哪里想要得到的parentId的呢?是不是传递给'Create'行动(这是有道理的,你的情况) –

+0

制作'public ActionResult Create(int ID)'方法并返回一个模型('ParentId'设置为'ID'的值),然后在视图中使用'@ Html.HiddenFor(m => m.ParentId)' –

回答

1

您可能想要通过ParentID从另一个Action Method,但我想知道为什么你想通过SelectList(如果没有选择那里),但那样做。

public ActionResult Create(int ParentID) 
{ 
    //I don't want a select list ParentID needs to be passed from the parent. 
    ViewBag.ParentID = ParentID; 
    return View(); 
} 

[HttpPost] 
[ValidateAntiForgeryToken] 
public ActionResult Create([Bind(Include = "ChildID,ParentID,ChildName")] Child child) 
{ 
    if (ModelState.IsValid) 
    { 
     //How do I set child.ParentID without a select list??? 
     db.Child.Add(child); 
     db.SaveChanges(); 
     return RedirectToAction("Index"); 
    } 

    return RedirectToAction("List"); 
} 

并在视图:

@Html.Hidden("ParentID", (object) ViewBag.ParentID) 

我认为这应该是要走的路。

+0

'@ Html.Hidden(“ParentID”,ViewBag .ParentID)'导致错误:HtmlHelper 没有名为'隐藏'的适用方法,但似乎有按该名称的扩展方法。扩展方法不能动态分派。考虑转换动态参数或调用扩展方法而不使用扩展方法语法。 – Dom

+0

也不能访问该id来传递'@ Html.ActionLink(“Create New Child”,“Create”,new {id = Model.SingleOrDefault()。Parent.ParentID})'as Model是一个空集合 – Dom

+0

Try将其转换为'object',因为它是动态表达式。请参阅编辑答案。 –

0

我认为你需要实现一个编辑动作(或更好的编辑动作一个获取和一个发布数据)。

然后,你应该定义你的场景..也就是说,你应该声明,如果你想有可能在编辑表单中改变你的子实体的当前“父母”,或者你希望当前的父母它是一旦孩子被修复实体已创建。

无论如何,您应该保持“创建”动作和视图没有变化。将parentID收集在下拉列表中尤为重要,因为此控件允许您在多个值中进行选择,并且在创建“子”实体时,可以说您需要在多个父母中进行选择。

至于有关“编辑”行为的关注,您可以执行下列动作:

public ActionResult Edit(int? cId) 
{ 

    if (id == null) 
    { 
     return new HttpStatusCodeResult(HttpStatusCode.BadRequest); 
    } 

    Child child = db.Child.Find(cId); 
    if (child == null) 
    { 
     return HttpNotFound(); 
    } 
    ViewBag.ParentID = new SelectList(db.Parents, "ParentID", "ParentName"); 
    return View(); 
} 

[HttpPost] 
[ValidateAntiForgeryToken] 
public ActionResult Edit([Bind(Include = "ChildID,ParentID,ChildName")] Child child) 
{ 
    if (ModelState.IsValid) 
    { 
     db.Child.Add(child); 
     db.SaveChanges(); 
     return RedirectToAction("Index"); 
    } 
    //You need this, because if you want change parent you can do it selecting from parents list 
    ViewBag.ParentID = new SelectList(db.Parents, "ParentID", "ParentName", child.ParentID); 
    return View(child); 
} 

如果你想避免修改你的孩子的当前父一旦它被分配第一次创建子实体,则需要在您的下拉列表控件添加一个“禁用”属性,因为它遵循..

@Html.DropDownList("ParentID", null, htmlAttributes: new { @disabled = "disabled" } })