Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.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
Asp.net mvc 4 DropDownList用于检索所选实体_Asp.net Mvc 4 - Fatal编程技术网

Asp.net mvc 4 DropDownList用于检索所选实体

Asp.net mvc 4 DropDownList用于检索所选实体,asp.net-mvc-4,Asp.net Mvc 4,我将值放入编辑框和下拉列表,然后按下输入按钮。之后,我在post back操作中获得了Rooms实体,但Rooms.Locations为null。我检查了ModelStatedictionary,在那里我看到了两个键的名称和位置以及两个正确的值。但我没有在房间里看到它。有什么不对劲吗 型号: namespace SimpleIM.Data { using System; using System.Collections.Generic; public partial class Roo

我将值放入编辑框和下拉列表,然后按下输入按钮。之后,我在post back操作中获得了Rooms实体,但Rooms.Locations为null。我检查了
ModelState
dictionary,在那里我看到了两个键的名称和位置以及两个正确的值。但我没有在房间里看到它。有什么不对劲吗

型号:

 namespace SimpleIM.Data
 {
 using System;
 using System.Collections.Generic;
 public partial class Rooms
 {
  public Rooms()
  {
   this.Employees = new HashSet<Employees>();
  }
  public int Id { get; set; }
  public string Name { get; set; }   
  public virtual ICollection<Employees> Employees { get; set; }
  public virtual Locations Locations { get; set; }
 }
}
视图:

@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()
房间

@LabelFor(model=>model.Name,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.Name,new{htmlAttributes=new{@class=“form control”})
@Html.DropDownListFor(model=>model.Locations,(IEnumerable)ViewData[“Locations”])
属性
位置
是一个复杂的对象,您无法绑定
(或任何其他表单控件)对于复杂对象。您不能将
SelectList
命名为与绑定到的属性相同的名称。您可以绑定到
Locations.Id
,但如果
Locations
包含任何具有验证属性的属性,则该操作将失败。请使用具有
int-SelectedLocation
IEnumerable LocationLis的视图模型最好的!谢谢你。。。
[HttpPost]
public ActionResult Create([Bind(Include = "Id, Name, Locations")] Rooms rooms)
{
 if (ModelState.IsValid)
 {
  db.RoomsSet.Add(rooms);
  db.SaveChanges();
  return RedirectToAction("Index");
 }
 ViewData["Locations"] = new SelectList(db.LocationsSet, "Id", "Name");
 return View();
}
@using (Html.BeginForm()) 
{
 @Html.AntiForgeryToken()
 <div class="form-horizontal">
 <h4>Rooms</h4>
 <hr />
    <div class="form-group">
        @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
        </div><br/>
        <div class="col-md-10">
            @Html.DropDownListFor(model => model.Locations, (IEnumerable<SelectListItem>) ViewData["Locations"])
        </div>
        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
</div>