Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用下拉列表创建编辑视图_C#_Asp.net_Asp.net Mvc_Entity Framework_Asp.net Mvc 3 - Fatal编程技术网

C# 如何使用下拉列表创建编辑视图

C# 如何使用下拉列表创建编辑视图,c#,asp.net,asp.net-mvc,entity-framework,asp.net-mvc-3,C#,Asp.net,Asp.net Mvc,Entity Framework,Asp.net Mvc 3,我拥有以下实体: public class Entidad { [Key] public int Id { get; set; } public string Nombre { get; set; } public virtual ICollection<Propiedad> Propiedades { get; set; } } public class Propiedad {

我拥有以下实体:

 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; }
    }

ViewBag.EntidadList
的类型错误,它必须是
IEnumerable
。我看不出您是如何构建它的,但作为一个示例,您可以在控制器中这样构建它:

var selectListItems = new List<SelectListItem>();

selectListItems.AddRange(
    someList.Select(item => new SelectListItem
    {
        Text = item.Name,
        Value = item.Id.ToString()
    }));
ViewBag.EntidadList = selectListItems;
var selectListItems=new List();
selectListItems.AddRange(
选择(item=>newselectListItem
{
Text=项目名称,
Value=item.Id.ToString()
}));
ViewBag.EntidadList=选择列表项;

您的
ViewBag.EntidadList的类型错误,它必须是
IEnumerable
。我看不出您是如何构建它的,但作为一个示例,您可以在控制器中这样构建它:

var selectListItems = new List<SelectListItem>();

selectListItems.AddRange(
    someList.Select(item => new SelectListItem
    {
        Text = item.Name,
        Value = item.Id.ToString()
    }));
ViewBag.EntidadList = selectListItems;
var selectListItems=new List();
selectListItems.AddRange(
选择(item=>newselectListItem
{
Text=项目名称,
Value=item.Id.ToString()
}));
ViewBag.EntidadList=选择列表项;

您的
ViewBag.EntidadList的类型错误,它必须是
IEnumerable
。我看不出您是如何构建它的,但作为一个示例,您可以在控制器中这样构建它:

var selectListItems = new List<SelectListItem>();

selectListItems.AddRange(
    someList.Select(item => new SelectListItem
    {
        Text = item.Name,
        Value = item.Id.ToString()
    }));
ViewBag.EntidadList = selectListItems;
var selectListItems=new List();
selectListItems.AddRange(
选择(item=>newselectListItem
{
Text=项目名称,
Value=item.Id.ToString()
}));
ViewBag.EntidadList=选择列表项;

您的
ViewBag.EntidadList的类型错误,它必须是
IEnumerable
。我看不出您是如何构建它的,但作为一个示例,您可以在控制器中这样构建它:

var selectListItems = new List<SelectListItem>();

selectListItems.AddRange(
    someList.Select(item => new SelectListItem
    {
        Text = item.Name,
        Value = item.Id.ToString()
    }));
ViewBag.EntidadList = selectListItems;
var selectListItems=new List();
selectListItems.AddRange(
选择(item=>newselectListItem
{
Text=项目名称,
Value=item.Id.ToString()
}));
ViewBag.EntidadList=选择列表项;

您可以使用以下代码

行动方法:

        // 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();
        }
在查看页面中:

     <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>
在编辑页面中:

     <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>

@LabelFor(model=>model.Entidad,新的{@class=“controllabel col-md-2”})
@DropDownListFor(m=>m.Entidad.Id,ViewBag.EntidadList作为SelectList,“selecciator”,new{@class=“form control”})

我希望这对您有所帮助。

您可以使用以下代码

行动方法:

        // 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();
        }
在查看页面中:

     <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>
在编辑页面中:

     <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>

@LabelFor(model=>model.Entidad,新的{@class=“controllabel col-md-2”})
@DropDownListFor(m=>m.Entidad.Id,ViewBag.EntidadList作为SelectList,“selecciator”,new{@class=“form control”})

我希望这对您有所帮助。

您可以使用以下代码

行动方法:

        // 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();
        }
在查看页面中:

     <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>
在编辑页面中:

     <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>

@LabelFor(model=>model.Entidad,新的{@class=“controllabel col-md-2”})
@DropDownListFor(m=>m.Entidad.Id,ViewBag.EntidadList作为SelectList,“selecciator”,new{@class=“form control”})

我希望这对您有所帮助。

您可以使用以下代码

行动方法:

        // 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();
        }
在查看页面中:

     <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>
在编辑页面中:

     <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>

@LabelFor(model=>model.Entidad,新的{@class=“controllabel col-md-2”})
@DropDownListFor(m=>m.Entidad.Id,ViewBag.EntidadList作为SelectList,“selecciator”,new{@class=“form control”})

我希望这能对您有所帮助。

您能显示ViewBag.EntidadList定义吗?您能对ViewBag.EntidadList进行查询吗?出现此错误的原因是
ViewBag.EntidadList
null
。在
Edit()
方法中,您不会将
SelectList
分配给
ViewBag.EntidadList
。您需要按照
Create()
方法执行此操作。是否可以显示ViewBag.EntidadList定义?是否可以对ViewBag.EntidadList进行查询?出现此错误的原因是
ViewBag.EntidadList
null
。在
Edit()
方法中,您不会将
SelectList
分配给
ViewBag.EntidadList
。您需要按照
Create()
方法执行此操作。是否可以显示ViewBag.EntidadList定义?是否可以对ViewBag.EntidadList进行查询?出现此错误的原因是
ViewBag.EntidadList
null
。在
Edit()
方法中,您不会将
SelectList
分配给
ViewBag.EntidadList
。您需要按照
Create()
方法执行此操作。是否可以显示ViewBag.EntidadList定义?是否可以对ViewBag.EntidadList进行查询?出现此错误的原因是
ViewBag.EntidadList
null
。在
Edit()
方法中,您不会将
SelectList
分配给
ViewBag.EntidadList
。您需要按照您的
Create()
方法进行操作。请检查更新,在“创建”菜单上如下所示:ViewBag.EntidadList=new SelectList(db.Entidades,“id”,“nombre”);请检查更新,创建如下内容:ViewBag.EntidadList=new SelectList(db.Entidades,“id”,“nombre”);请检查更新,创建如下内容:ViewBag.EntidadList=new SelectList(db.Entidades,“id”,“nombre”);请检查更新,创建如下内容:ViewBag.EntidadList=new SelectList(db.Entidades,“id”,“nombre”);