在实体的编辑页面中填充 select 框
Populate select box within the edit page of an entity
我创建了一个 asp.net MVC 应用程序,它使用 entity framework 作为 "fun"。
我有两个实体,一个用于用户,一个用于教师(学校)。每个用户都有一个需要能够改变的教员。我想使用 select 框来执行此操作,其中显示了每个教员的姓名。最后的 HTML 应该是这个样子:
<select>
<option>Faculty name 1</option>
<option>Faculty name 2</option>
<option>Faculty name 3</option>
<select>
下面的代码当前在带有数字类型的文本框中显示教师的 id 值。此 ID 是用户实体的一部分,当前未从教师实体中获取。
我不确定如何
- 将院系列表传入HTML进行显示
- 将 selected 教员存储在用户实体教员变量中
Edit.cshtml
@model Purely_Servers.Models.user
<div class="form-group col-md-12">
<div class="col-md-8">
@Html.EditorFor(model => model.school, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.school, "", new { @class = "text-danger" })
</div>
</div>
用户控制器
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using Purely_Servers.DAL;
using Purely_Servers.Models;
namespace Purely_Servers.Controllers
{
public class usersController : Controller
{
private serverContext db = new serverContext();
// GET: users
public ActionResult Index()
{
return View(db.Users.ToList());
}
// GET: users/Details/5
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
user user = db.Users.Find(id);
if (user == null)
{
return HttpNotFound();
}
return View(user);
}
// GET: users/Create
public ActionResult Create()
{
return View();
}
// POST: users/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 = "id,first_name,last_name,email,house_number,street,city,postcode,authorised,school,archived")] user user)
{
if (ModelState.IsValid)
{
db.Users.Add(user);
// find the users school in the database, add the user to the faculty's collection
var faculty = db.Faculties.Find(user.school);
user.faculty = faculty;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(user);
}
// GET: users/Edit/5
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
user user = db.Users.Find(id);
if (user == null)
{
return HttpNotFound();
}
return View(user);
}
// POST: users/Edit/5
// 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 Edit([Bind(Include = "id,first_name,last_name,email,house_number,street,city,postcode,authorised,school,archived")] user user)
{
if (ModelState.IsValid)
{
// find the users school in the database, add the user to the faculty's collection
var faculty = db.Faculties.Find(user.school); // get the value from the page
user.faculty = faculty;
db.Entry(user).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(user);
}
// GET: users/Delete/5
public ActionResult Delete(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
user user = db.Users.Find(id);
if (user == null)
{
return HttpNotFound();
}
return View(user);
}
// POST: users/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
user user = db.Users.Find(id);
db.Users.Remove(user);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}
}
用户模型
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
namespace Purely_Servers.Models
{
[Table("userTable")]
public class user
{
[Key]
public int id { get; set; }
public string first_name { get; set; }
public string last_name { get; set; }
public string email { get; set; }
public int house_number { get; set; }
public string street { get; set; }
public string city { get; set; }
public string postcode { get; set; }
public int authorised { get; set; }
public int school { get; set; }
public int archived { get; set; }
public virtual faculty faculty { get; set; }
}
}
非常感谢任何帮助
主要变化是您需要将模型传递给视图,而不仅仅是 User
实体。
创建模型:
public class EditUserModel
{
public user User{get; set;}
public SelectList FacultyList{get; set;}
}
控制器编辑方法:
public ActionResult Edit(int? id)
{
var model = new EditUserModel
{
User = db.Users.Find(id),
FacultyList = (from f in db.Faculties
select new SelectListItem
{
Value = f.Id.ToString(),
Text = f.Name
}).ToList();
};
return View(model);
}
查看:
@model EditUserModel
<div>
<div >
@Html.EditorFor(model => model.User.school, Model.FacultyList )
</div>
</div>
还有很多地方需要改进,但这是朝着正确方向迈出的一步。
我创建了一个 asp.net MVC 应用程序,它使用 entity framework 作为 "fun"。
我有两个实体,一个用于用户,一个用于教师(学校)。每个用户都有一个需要能够改变的教员。我想使用 select 框来执行此操作,其中显示了每个教员的姓名。最后的 HTML 应该是这个样子:
<select>
<option>Faculty name 1</option>
<option>Faculty name 2</option>
<option>Faculty name 3</option>
<select>
下面的代码当前在带有数字类型的文本框中显示教师的 id 值。此 ID 是用户实体的一部分,当前未从教师实体中获取。
我不确定如何
- 将院系列表传入HTML进行显示
- 将 selected 教员存储在用户实体教员变量中
Edit.cshtml
@model Purely_Servers.Models.user
<div class="form-group col-md-12">
<div class="col-md-8">
@Html.EditorFor(model => model.school, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.school, "", new { @class = "text-danger" })
</div>
</div>
用户控制器
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using Purely_Servers.DAL;
using Purely_Servers.Models;
namespace Purely_Servers.Controllers
{
public class usersController : Controller
{
private serverContext db = new serverContext();
// GET: users
public ActionResult Index()
{
return View(db.Users.ToList());
}
// GET: users/Details/5
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
user user = db.Users.Find(id);
if (user == null)
{
return HttpNotFound();
}
return View(user);
}
// GET: users/Create
public ActionResult Create()
{
return View();
}
// POST: users/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 = "id,first_name,last_name,email,house_number,street,city,postcode,authorised,school,archived")] user user)
{
if (ModelState.IsValid)
{
db.Users.Add(user);
// find the users school in the database, add the user to the faculty's collection
var faculty = db.Faculties.Find(user.school);
user.faculty = faculty;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(user);
}
// GET: users/Edit/5
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
user user = db.Users.Find(id);
if (user == null)
{
return HttpNotFound();
}
return View(user);
}
// POST: users/Edit/5
// 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 Edit([Bind(Include = "id,first_name,last_name,email,house_number,street,city,postcode,authorised,school,archived")] user user)
{
if (ModelState.IsValid)
{
// find the users school in the database, add the user to the faculty's collection
var faculty = db.Faculties.Find(user.school); // get the value from the page
user.faculty = faculty;
db.Entry(user).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(user);
}
// GET: users/Delete/5
public ActionResult Delete(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
user user = db.Users.Find(id);
if (user == null)
{
return HttpNotFound();
}
return View(user);
}
// POST: users/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
user user = db.Users.Find(id);
db.Users.Remove(user);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}
}
用户模型
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
namespace Purely_Servers.Models
{
[Table("userTable")]
public class user
{
[Key]
public int id { get; set; }
public string first_name { get; set; }
public string last_name { get; set; }
public string email { get; set; }
public int house_number { get; set; }
public string street { get; set; }
public string city { get; set; }
public string postcode { get; set; }
public int authorised { get; set; }
public int school { get; set; }
public int archived { get; set; }
public virtual faculty faculty { get; set; }
}
}
非常感谢任何帮助
主要变化是您需要将模型传递给视图,而不仅仅是 User
实体。
创建模型:
public class EditUserModel
{
public user User{get; set;}
public SelectList FacultyList{get; set;}
}
控制器编辑方法:
public ActionResult Edit(int? id)
{
var model = new EditUserModel
{
User = db.Users.Find(id),
FacultyList = (from f in db.Faculties
select new SelectListItem
{
Value = f.Id.ToString(),
Text = f.Name
}).ToList();
};
return View(model);
}
查看:
@model EditUserModel
<div>
<div >
@Html.EditorFor(model => model.User.school, Model.FacultyList )
</div>
</div>
还有很多地方需要改进,但这是朝着正确方向迈出的一步。