2014-08-28 42 views
0

淨MVC如何TE通過傳遞一個多對多的表得到一個表的字段

如果創建了CLAS項目,projectUser和用戶

我想通過選擇項目的ID來創建一個projectUser而id從用戶無論是在一個下拉列表

我projectUser控制器

// GET: /ProjectUser/Create 
    public ActionResult Create() 
    { 
     ViewBag.UserID = new SelectList(db.Users, "Id", "UserName"); 
     ViewBag.ProjectID = new SelectList(db.Projects, "Id", "ProjectID"); 
     return View(); 
    } 

    // POST: /ProjectUser/Create 
    // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
    // more details see http://go.microsoft.com/fwlink/?LinkId=317598. 
    [HttpPost] 
    [ValidateAntiForgeryToken] 
    public ActionResult Create([Bind(Include="ProjectUserID")] ProjectUser projectuser) 
    { 
     if (ModelState.IsValid) 
     { 
      db.ProjectUsers.Add(projectuser); 
      db.SaveChanges(); 
      return RedirectToAction("Index"); 
     } 
     ViewBag.UserID = new SelectList(db.Users, "Id", "UserName", projectuser.User.Id); 
     ViewBag.ProjectID = new SelectList(db.Projects, "Id", "ProjectID", projectuser.Projects.ProjectID); 
     return View(projectuser); 
    } 

我的觀點

<div class="form-horizontal"> 
    <h4>ProjectUser</h4> 
    <hr /> 
    @Html.ValidationSummary(true) 
    <div class="form-group"> 
     @Html.LabelFor(model => model.Projects, "Kies project", new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      @Html.DropDownList("ProjectID", String.Empty) 
      @Html.ValidationMessageFor(model => model.Projects) 
     </div> 
    </div> 
    <div class="form-group"> 
     @Html.LabelFor(model => model.User, "Kies user", new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      @Html.DropDownList("Id", String.Empty) 
      @Html.ValidationMessageFor(model => model.User) 
     </div> 
    </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> 

我projectUser類

public class ProjectUser 
    { 

     public int ProjectUserID { get; set; } 
     public virtual ApplicationUser User { get; set; } 
     public virtual Project Projects { get; set; } 
    } 

回答

-1

你應該有ApplicationUserId和專案在ProjectUser類。 然後你可以簡單地通過綁定id來添加一個ProjectUser。

相關問題