只是爲了序言,我是一個完整的菜鳥,需要我能得到的所有幫助。如何選擇從其他表格的下拉列表中存儲數據,或手動輸入數據?
我目前正在製作一個存儲條目的Web應用程序數據庫。它會存儲員工和角色信息。我正在使用一個EntityFramework與asp.net MVC設置。下面是我的數據庫中的表
Staff
+--=-----+----------+
| id | int |
| name | string |
| role | string |
+--------+----------+
Role
+--=-----+----------+
| id | int |
| name | string |
+--------+----------+
當爲員工創造條目,其角色欄,用戶將不得不讀取由角色表,或手動輸入角色值填充值的下拉列表選擇。
如果用戶使用下拉列表中的值,則該值的名稱(角色名稱)將作爲Staff角色中的字符串存儲,而不是id。
如果用戶選擇手動輸入角色列的值,那麼在創建職員條目後,該值將自動添加到角色表中。
從外觀上看,這是我想要做的事:
https://i.stack.imgur.com/GfATW.png
https://i.stack.imgur.com/xAiR0.png
這是我爲我在create.html上文件代碼職員
<div class="form-group">
@Html.LabelFor(model => model.role, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("RoleList", null, htmlAttributes: new { @class = "form-control" })
@Html.EditorFor(model => model.role, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.role, "", new { @class = "text-danger" })
</div>
</div>
它缺少像複選框這樣的東西,但我被困在如何正確地輸入它來做我正在尋找的功能。
這裏是爲員工創造部分我控制器代碼:
// GET: Staff/Create
public ActionResult Create()
{
ViewBag.RoleList = new SelectList(db.Roles, "roleid", "roleName");//
return View();
}
// POST: Staff/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 = "staffid,staffName,org,role")] Staff staff)
{
if (ModelState.IsValid)
{
db.Staves.Add(staff);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.RoleList = new SelectList(db.Roles, "roleid", "roleName");//
return View(staff);
}
感謝您的幫助!讓我知道你是否需要更多信息。