Asp.net mvc 3 MVC3实体框架4.1RC@Html.DropDownListFor实际如何工作?
好吧,我已经看过谷歌、StackOverflow和ASP.net了——我真的是唯一一个不懂这些的人吗 Rough problem=我有一个引用办公室实体的员工实体。我以前能够创建新员工(因为我忘记了代码是如何工作的,所以狠狠地揍了我一顿),但现在我既不能创建员工,也不能编辑现有员工 现在,这是我学到的; 1) 确保在每一步都将Office列表添加到ViewBag中 2) 包括失败的发布/编辑;调用函数以使用Office列表重新填充ViewBag 3) 我认为(!!)您总是希望设置Employee.Office,而不是Employee.Office.OfficeID;后者会导致“是对象关键信息的一部分,无法修改”错误 所以,我所拥有的是 具有以下方法的控制器Asp.net mvc 3 MVC3实体框架4.1RC@Html.DropDownListFor实际如何工作?,asp.net-mvc-3,entity-framework-4.1,Asp.net Mvc 3,Entity Framework 4.1,好吧,我已经看过谷歌、StackOverflow和ASP.net了——我真的是唯一一个不懂这些的人吗 Rough problem=我有一个引用办公室实体的员工实体。我以前能够创建新员工(因为我忘记了代码是如何工作的,所以狠狠地揍了我一顿),但现在我既不能创建员工,也不能编辑现有员工 现在,这是我学到的; 1) 确保在每一步都将Office列表添加到ViewBag中 2) 包括失败的发布/编辑;调用函数以使用Office列表重新填充ViewBag 3) 我认为(!!)您总是希望设置Employee
private void AddOfficesToViewBag()
{
Dictionary<string, Office> list = new Dictionary<string, Office>();
foreach (Office office in company.GetAllOffices())
list.Add(office.ToString(), office);
SelectList items = new SelectList(list, "Value", "Key");
ViewBag.OfficeList = items;
}
我将选择编辑视图,它与创建视图几乎相同
@使用(Html.BeginForm()){
@Html.ValidationSummary(true)
雇员
@Html.HiddenFor(model => model.EmployeeID)
<div class="editor-label">
@Html.LabelFor(model => model.Office)
</div>
<div class="editor-field">
@Html.DropDownListFor(model => model.Office, (SelectList) ViewBag.OfficeList)
@Html.ValidationMessageFor(model => model.Office)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Age)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Age)
@Html.ValidationMessageFor(model => model.Age)
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
@Html.HiddenFor(model=>model.EmployeeID)
@LabelFor(model=>model.Office)
@DropDownListFor(model=>model.Office,(SelectList)ViewBag.OfficeList)
@Html.ValidationMessageFor(model=>model.Office)
@LabelFor(model=>model.Name)
@EditorFor(model=>model.Name)
@Html.ValidationMessageFor(model=>model.Name)
@LabelFor(model=>model.Age)
@EditorFor(model=>model.Age)
@Html.ValidationMessageFor(model=>model.Age)
}
我想说的是,编辑,特别是,看起来几乎没有。它设法绑定到传入的Employee对象,并将下拉列表设置为适当的条目。
查看原始HTML源代码显示输出值是Office.ToString()值。
对我来说,奇怪的是,一些神奇的事情正在发生,它将Employee->Office绑定到正确的条目,这使得编辑视图可以工作,但是所选项目(字符串,也称为object->ToString())没有相应地转换到原始列表
这看起来很基本(MVC/EF4/DropDownList),我觉得我遗漏了一些非常基本的东西
感谢所有的想法。
当做
Scott基于以下内容,您可以 请执行以下操作:
[HttpPost]
public ActionResult Edit(Guid id, FormCollection collection)
{
CollectionViewModel cvc = new CollectionViewModel();
cvc.Collection = _db.Collections.Where(c => c.CollectionId == id).Include("CollectionType").First();
Guid collectionTypeId = Guid.Parse(collection["CollectionTypeId"].ToString());
cvc.Collection.CollectionType =_db.CollectionTypes.Where(ct =>ct.CollectionTypeId == collectionTypeId).First();
if (TryUpdateModel(cvc))
{
_db.SaveChanges();
return RedirectToAction("Index");
}
}
视图模型
public class CollectionViewModel
{
public Collection Collection {get; set; }
public Guid CollectionTypeId { get; set; }
public SelectList CollectionTypes { get; set; }
}
[HttpPost]
public ActionResult Edit(Guid id, FormCollection collection)
{
CollectionViewModel cvc = new CollectionViewModel();
cvc.Collection = _db.Collections.Where(c => c.CollectionId == id).Include("CollectionType").First();
Guid collectionTypeId = Guid.Parse(collection["CollectionTypeId"].ToString());
cvc.Collection.CollectionType =_db.CollectionTypes.Where(ct =>ct.CollectionTypeId == collectionTypeId).First();
if (TryUpdateModel(cvc))
{
_db.SaveChanges();
return RedirectToAction("Index");
}
}
public class CollectionViewModel
{
public Collection Collection {get; set; }
public Guid CollectionTypeId { get; set; }
public SelectList CollectionTypes { get; set; }
}