我创建了一个asp.net MVC应用程序,该应用程序使用实体框架来实现" fun"。
我有两个实体,一个用于用户,一个用于教师(学校)。每个用户都有一个需要能够改变的教师。我想使用一个显示每个教员姓名的选择框来执行此操作。最终的HTML应如下所示:
<select>
<option>Faculty name 1</option>
<option>Faculty name 2</option>
<option>Faculty name 3</option>
<select>
以下代码当前在类型为number的文本框中显示教师的id值。此Id是用户实体的一部分,目前尚未从教师实体中获取。
我不确定如何
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>
usersController
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; }
}
}
任何帮助非常感谢
答案 0 :(得分:1)
主要的变化是您需要将模型传递给您的视图,而不仅仅是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>
要做出很多改进,但这是朝着正确方向迈出的一步。