我在使用存儲庫,視圖模型和映射到我的ASP.Net MVC項目中實現CRUD操作時遇到問題。 「細節」(讀取關於一個對象的信息)和「索引」(讀取整個對象列表),控制器正在工作。使用存儲庫和映射實現MVC設計模式,C#
我將Model
映射到ViewModel
,然後在View
中顯示它。但對於Create
,Update
和Delete
操作,我應該將ViewModel
映射到Model
。你能告訴我我錯在哪裏嗎?
模型
public class User
{
[Key]
public int Id { get; set; }
[Required]
public string Name { get; set; }
[Unique]
[Required]
[MaxLength(100)]
public string Email { get; set; }
[Required]
public string Password { get; set; }
public string Phone { get; set; }
public bool IsAdmin { get; set; }
}
基地庫
public class BaseRepository<T> : IBaseRepository<T> where T : class
{
private RushHourContext db = null;
private DbSet<T> table = null;
public BaseRepository()
{
this.db = new RushHourContext();
table = db.Set<T>();
}
public BaseRepository(RushHourContext db)
{
this.db = db;
table = db.Set<T>();
}
public IEnumerable<T> SelectAll()
{
return table.ToList();
}
public T SelectByID(object id)
{
return table.Find(id);
}
public void Insert(T obj)
{
table.Add(obj);
}
public void Update(T obj)
{
table.Attach(obj);
db.Entry(obj).State = EntityState.Modified;
}
public void Delete(object id)
{
T existing = table.Find(id);
table.Remove(existing);
}
public void Save()
{
db.SaveChanges();
}
}
接口庫
public interface IBaseRepository<T> where T : class
{
IEnumerable<T> SelectAll();
T SelectByID(object id);
void Insert(T obj);
void Update(T obj);
void Delete(object id);
void Save();
}
控制器
private RushHourContext _db = new RushHourContext();
private IBaseRepository<User> _repository = null;
public UsersController()
{
this._repository = new BaseRepository<User>();
}
public ActionResult Index()
{
if (!LoginUserSession.IsStateAdmin)
{
return RedirectToAction("Login");
}
var users = _repository.SelectAll().ToList();
var userViewModel = Mapper.Map<List<UserViewModel>>(users);
return View(userViewModel);
}
public ActionResult Details(int? id)
{
var users = _repository.SelectByID(id);
var userViewModel = Mapper.Map<UserViewModel>(users);
return View(userViewModel);
}
public ActionResult Create(User user)
{
var users = _repository.Insert(user); // THIS CODE HERE IS WRONG
var userViewModel = Mapper.Map<User>(users);
return View(userViewModel);
}
UserViewModel
public class UserViewModel
{
public int Id { get; set; }
[Required(ErrorMessage = "Please enter User Name.")]
[Display(Name = "User Name")]
public string Name { get; set; }
[MaxLength(100)]
[Display(Name = "Email Address")]
public string Email { get; set; }
[Required]
[Display(Name = "Password")]
public string Password { get; set; }
public string Phone { get; set; }
public bool IsAdmin { get; set; }
}
查看
@model RushHour.ViewModels.UserViewModel
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
<div>
@Html.AntiForgeryToken()
@using (Html.BeginForm("Create", "Users", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
<div>@Html.LabelFor(model => model.Id)</div>
<div>@Html.TextBoxFor(model => model.Id)</div>
<div>@Html.ValidationMessageFor(model => model.Id)</div>
<div>@Html.LabelFor(model => model.Name)</div>
<div>@Html.TextBoxFor(model => model.Name)</div>
<div>@Html.ValidationMessageFor(model => model.Name)</div>
<div>@Html.LabelFor(model => model.Password)</div>
<div>@Html.TextBoxFor(model => model.Password)</div>
<div>@Html.ValidationMessageFor(model => model.Password)</div>
<div>@Html.LabelFor(model => model.Email)</div>
<div>@Html.TextBoxFor(model => model.Email)</div>
<div>@Html.ValidationMessageFor(model => model.Email)</div>
<div>@Html.LabelFor(model => model.Phone)</div>
<div>@Html.TextBoxFor(model => model.Phone)</div>
<div>@Html.ValidationMessageFor(model => model.Phone)</div>
<div>@Html.LabelFor(model => model.IsAdmin)</div>
<div>@Html.TextBoxFor(model => model.IsAdmin)</div>
<div>@Html.ValidationMessageFor(model => model.IsAdmin)</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>
}
</div>
「我要地圖視圖模型到模型中。」 - 正是 - 做到這一點。您遇到的問題是什麼?通常,有一個DTO,例如'UserDto',它以表單數據的形式出現。然後將其映射到您的「用戶」並重定向到該新創建索引的詳細信息頁面。 – ironstone13
你能給我一些例子或鏈接。這是我第一次這樣做,我迷路了。我評論我的問題在哪裏。在我的創建控制器// var users = _repository.Insert(user); – Geya
對於create函數,使用'UserViewModel'而不是'User',然後映射到'User'以保存到數據庫。 –