在HTML帮助器下拉列表中,如何获取外键数据?

时间:2017-05-20 14:54:11

标签: c# asp.net asp.net-mvc

我是ASP.NET新手。我的表格看起来像这样

enter image description here

此代码在表单

中显示角色
Benchmark         Mode  Cnt   Score   Error  Units
MyBenchmark.str   avgt   20  47.695 ± 1.869  ns/op
MyBenchmark.tsts  avgt   20   6.999 ± 0.191  ns/op
控制器中的

  @Html.DropDownList("id", (IEnumerable<SelectListItem>)ViewBag.lis, null, new { @class = "form-control" })

角色等级

  public ActionResult register()
    {      // 
        ViewBag.lis = new SelectList(new dbdemoEntities().Roles, "id", "name");
        return View();
    }

注册课程

  public partial class Role
{
    public int Id { get; set; }
    public string name { get; set; }

    public virtual Register Register { get; set; }
}

问题是我可以获取除Role之外的所有数据。该角色为空。我如何获得角色ID?

   public partial class Register
{
    public int Id { get; set; }
    public string name { get; set; }
    public string email { get; set; }
    public string password { get; set; }
    public Nullable<int> phone_no { get; set; }

    public virtual Role Role { get; set; }
}

我认为问题是我应该写model =&gt; model.role就像这里的名字的例子。

   [HttpPost]
    public ActionResult register(Register obj)
    {
        using(var db = new dbdemoEntities())
        {
            var data = new Register()
            {

                email = obj.email,
                name = obj.name,
                password = obj.password,
                phone_no = obj.phone_no,
                Role = obj.Role
            };

            db.Registers.Add(data);
            db.SaveChanges();
            ViewBag.register = "Your account has been registered!";
        }

        return PartialView();
    }

这是我现在更新的内容

 @Html.EditorFor(model => model.name, new { htmlAttributes = new { @class = "form-control" } })

在HTML中

enter image description here

更新后的问题:

enter image description here

更改后

    ViewBag.lis = new SelectList(new dbdemoEntities().Roles, "Id", "name");

这是另一个错误

enter image description here

2 个答案:

答案 0 :(得分:1)

试试这个:

更改:ViewBag.lis = new SelectList(new dbdemoEntities().Roles, "id", "name");

 ViewBag.lis = new SelectList(new dbdemoEntities().Roles, "Id", "name");

然后:

@Html.DropDownList(model => model.Role.Id, (IEnumerable<SelectListItem>)ViewBag.lis, null, new { @class = "form-control" })

并为注册类创建构造函数:

public partial class Register
{
    public Register()
    {
        this.Role = new Role();
    }
    public int Id { get; set; }
    public string name { get; set; }
    public string email { get; set; }
    public string password { get; set; }
    public Nullable<int> phone_no { get; set; }

    public virtual Role Role { get; set; }
}

====== 更新 =======

更改此操作:

[HttpPost]
    public ActionResult register(Register obj)
    {
        using(var db = new dbdemoEntities())
        {
            var data = new Register()
            {
                email = obj.email,
                name = obj.name,
                password = obj.password,
                phone_no = obj.phone_no,
                Role = db.Roles.Single(r=> r.Id == obj.Role.Id)
            };

            db.Registers.Add(data);
            db.SaveChanges();
            ViewBag.register = "Your account has been registered!";
        }

        return PartialView();
    }

答案 1 :(得分:0)

如果您想发布Register.Id属性,请更改为此:

@Html.DropDownListFor(model => model.Id,(SelectList) ViewBag.list,new { @class="form-control"})