Tôi muốn biết làm cách nào để sử dụng ViewModel của mình trên Tạo hành động? Tôi đã thử một số ví dụ tôi tìm thấy ở đây trong diễn đàn, nhưng không ai giải quyết được vấn đề của tôi. Tôi đã quên não trong vài ngày, nhưng không thể hiểu được điều gì là sai.MVC ViewModel Error - Không có hàm tạo tham số nào được định nghĩa cho đối tượng này
Bất cứ khi nào tôi nhấp vào nút Tạo, tôi nhận được lỗi sau: Không có hàm tạo tham số nào được xác định cho đối tượng này.
@model MvcMusicStore.ViewModels.AlbumViewModel
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Album</legend>
<div class="editor-label">
@Html.LabelFor(model => model.AlbumItem.GenreId, "Genre")
</div>
<div class="editor-field">
@Html.DropDownList("Genres", String.Empty)
@Html.ValidationMessageFor(model => model.AlbumItem.GenreId)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.AlbumItem.ArtistId, "Artist")
</div>
<div class="editor-field">
@Html.DropDownList("Artists", String.Empty)
@Html.ValidationMessageFor(model => model.AlbumItem.ArtistId)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.AlbumItem.Title)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.AlbumItem.Title)
@Html.ValidationMessageFor(model => model.AlbumItem.Title)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.AlbumItem.Price)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.AlbumItem.Price)
@Html.ValidationMessageFor(model => model.AlbumItem.Price)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.AlbumItem.AlbumArtUrl)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.AlbumItem.AlbumArtUrl)
@Html.ValidationMessageFor(model => model.AlbumItem.AlbumArtUrl)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
Create.cshtml
public class StoreManagerController : Controller
{
private MusicStoreDB db = new MusicStoreDB();
//
// GET: /StoreManager/Create
public ActionResult Create()
{
var viewModel = new AlbumViewModel()
{
Genres = new SelectList(db.Genres, "GenreId", "Name"),
Artists = new SelectList(db.Artists, "ArtistId", "Name")
};
return View(viewModel);
}
//
// POST: /StoreManager/Create
[HttpPost]
public ActionResult Create(AlbumViewModel vm)
{
if (ModelState.IsValid)
{
db.Albums.Add(vm.AlbumItem);
db.SaveChanges();
return RedirectToAction("Index");
}
vm.Genres = new SelectList(db.Genres, "GenreId", "Name", vm.AlbumItem.GenreId);
vm.Artists = new SelectList(db.Artists, "ArtistId", "Name", vm.AlbumItem.ArtistId);
return View(vm);
}
}
StoreManager.cs - Snippet
public class AlbumViewModel
{
public AlbumViewModel()
{
// nothing
}
public Album AlbumItem { get; set; }
public SelectList Genres { get; set; }
public SelectList Artists { get; set; }
}
public class Album
{
public Album()
{
// nothing
}
public virtual int AlbumId { get; set; }
public virtual int GenreId { get; set; }
public virtual int ArtistId { get; set; }
public virtual string Title { get; set; }
public virtual decimal Price { get; set; }
public virtual string AlbumArtUrl { get; set; }
public virtual Genre Genre { get; set; }
public virtual Artist Artist { get; set; }
}
public class Artist
{
public Artist()
{
// nothing
}
public virtual int ArtistId { get; set; }
public virtual string Name { get; set; }
}
public class Genre
{
public Genre()
{
// nothing
}
public virtual int GenreId { get; set; }
public virtual string Name { get; set; }
public virtual string Description { get; set; }
public virtual List<Album> Albums { get; set; }
}
'Genre' là gì? – SLaks
Đã thêm 'Genre' và' Artist' trên mã ở trên. – fvss
Điều duy nhất dường như không có một hàm tạo tham số là SelectList (http://msdn.microsoft.com/en-us/library/system.web.mvc.selectlist(v=vs.108).aspx). Nếu bạn bình luận thì sao? –