如何创建编辑视图与一个DropDownList [英] How to create edit view with a dropdownlist
本文介绍了如何创建编辑视图与一个DropDownList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下实体:
公共类Entidad
{
[键]
公众诠释标识{搞定;组; }
公共字符串农布雷{搞定;组; } 公共虚拟的ICollection< Propiedad> Propiedades {搞定;组; }
}
公共类Propiedad
{
[键]
公众诠释标识{搞定;组; } 公共虚拟Entidad Entidad {搞定;组; } 公共字符串Codigo {搞定;组; }
公共字符串农布雷{搞定;组; }
公共字符串TipoDeDatos {搞定;组; }
}
和我的编辑视图
< DIV CLASS =表单组>
@ Html.LabelFor(型号=> model.Entidad,新{@class =控制标签COL-MD-2})
< DIV CLASS =COL-MD-10>
@ Html.DropDownListFor(M = GT; m.Entidad.Id,(的SelectList)(ViewBag.EntidadList),Seleccionar,新{@class =表格控})
< / DIV>
< / DIV>
不过,我得到了这个观点错误
这有钥匙的ViewData的项目'Entidad.Id的类型为System.Int32,但必须是类型为IEnumerable的。
说明
我的控制器:
// GET:/ GlobalAdmin / Propiedades /创建
公众的ActionResult的Create()
{
ViewBag.EntidadList =新的SelectList(db.Entidades,ID,农布雷);
返回查看();
}
公众的ActionResult编辑(INT?ID)
{
如果(ID == NULL)
{
返回新的HTTPStatus codeResult(的HTTPStatus code.BadRequest);
}
Propiedad propiedad = db.Propiedades.Find(ID);
如果(propiedad == NULL)
{
返回HttpNotFound();
}
返回查看(propiedad);
}
解决方案
您可以使用以下code。
在操作方法:
// GET:/ GlobalAdmin / Propiedades /创建
公众的ActionResult的Create()
{
ViewBag.EntidadList =
新的SelectList(_db.Entidades.ToList()。
选择(X =>新建{ID = x.Id,nomber = x.Nombre}),ID,农布雷); // Viewbag
返回查看();
}
在视图页面:
< DIV CLASS =表单组>
@ Html.LabelFor(型号=> model.Entidad,新{@class =控制标签COL-MD-2})
< DIV CLASS =COL-MD-10>
@ Html.DropDownListFor(M = GT; m.Entidad.Id,ViewBag.EntidadList为的SelectList,Seleccionar,新{@class =表格控})
< / DIV>
< / DIV>
如果你想使用编辑视图中的DropDownList和您可以使用下面的code数据库选择的项目:
// GET:/ GlobalAdmin / Propiedades /创建
公众的ActionResult编辑(INT?ID)
{
如果(ID == NULL)
{
返回新的HTTPStatus codeResult(的HTTPStatus code.BadRequest);
}
Propiedad propiedad = db.Propiedades.Find(ID);
如果(propiedad == NULL)
{
返回HttpNotFound();
}
ViewBag.EntidadList =
新的SelectList(_db.Entidades.ToList()。
选择(X =>新建{ID = x.Id,nomber = x.Nombre}),ID,农布雷,则必须从Entidad获得ID和在这里设置); // Viewbag
返回查看(propiedad);
}
在编辑页面:
< DIV CLASS =表单组>
@ Html.LabelFor(型号=> model.Entidad,新{@class =控制标签COL-MD-2})
< DIV CLASS =COL-MD-10>
@ Html.DropDownListFor(M = GT; m.Entidad.Id,ViewBag.EntidadList为的SelectList,Seleccionar,新{@class =表格控})
< / DIV>
< / DIV>
我希望这帮助你。
I have the following entities:
public class Entidad
{
[Key]
public int Id { get; set; }
public string Nombre { get; set; }
public virtual ICollection<Propiedad> Propiedades { get; set; }
}
public class Propiedad
{
[Key]
public int Id { get; set; }
public virtual Entidad Entidad { get; set; }
public string Codigo { get; set; }
public string Nombre { get; set; }
public string TipoDeDatos { get; set; }
}
and on my edit view
<div class="form-group">
@Html.LabelFor(model => model.Entidad, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(m => m.Entidad.Id, (SelectList)(ViewBag.EntidadList), "Seleccionar", new { @class = "form-control" })
</div>
</div>
However I get this error on the view
The ViewData item that has the key 'Entidad.Id' is of type 'System.Int32' but must be of type 'IEnumerable'. Description
my controllers are:
// GET: /GlobalAdmin/Propiedades/Create
public ActionResult Create()
{
ViewBag.EntidadList = new SelectList(db.Entidades, "id", "nombre");
return View();
}
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Propiedad propiedad = db.Propiedades.Find(id);
if (propiedad == null)
{
return HttpNotFound();
}
return View(propiedad);
}
解决方案
You can using the following code.
In action method :
// GET: /GlobalAdmin/Propiedades/Create
public ActionResult Create()
{
ViewBag.EntidadList =
new SelectList(_db.Entidades.ToList().
Select(x => new { id= x.Id, nomber= x.Nombre }), "Id", "nombre "); // Viewbag
return View();
}
In view page :
<div class="form-group">
@Html.LabelFor(model => model.Entidad, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(m => m.Entidad.Id, ViewBag.EntidadList as SelectList, "Seleccionar", new { @class = "form-control" })
</div>
</div>
If you want use the dropdownlist in edit view and select the item from database you can use the following code :
// GET: /GlobalAdmin/Propiedades/Create
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Propiedad propiedad = db.Propiedades.Find(id);
if (propiedad == null)
{
return HttpNotFound();
}
ViewBag.EntidadList =
new SelectList(_db.Entidades.ToList().
Select(x => new { id= x.Id, nomber= x.Nombre }), "Id", "nombre ",you must get id from Entidad and set here ); // Viewbag
return View(propiedad);
}
In edit page :
<div class="form-group">
@Html.LabelFor(model => model.Entidad, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(m => m.Entidad.Id, ViewBag.EntidadList as SelectList, "Seleccionar", new { @class = "form-control" })
</div>
</div>
I hope this help you.
这篇关于如何创建编辑视图与一个DropDownList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文