ASP.Net MVCプロジェクトでリポジトリ、ビューモデル、マッピングを使用してCRUD操作を実装する際に問題があります。 「詳細」(1つのオブジェクトに関する情報を読む)と「インデックス」(オブジェクトのリスト全体を読む)は、コントローラが動作しています。リポジトリとマッピングを使用して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>
"私がモデルにViewModelに地図すべきである。" - 正確に - ちょうどそれを行います。あなたが経験している問題は何ですか?通常、DTOがあります。例えば、フォームデータとして入ってくる「UserDto」と言います。 'User'にマップし、新しく作成したインデックスの詳細ページにリダイレクトします。 – ironstone13
いくつかの例やリンクを教えてください。それは私が初めてそれをして、私は迷っています。私はどこに私の問題があるとコメントします。私の作成コントローラでは、// var users = _repository.Insert(user); – Geya
create関数では 'User'の代わりに' UserViewModel'を使い、 'User'に写像してデータベースに保存します。 –