私は作成ビューを持っています:-
@model MvcCommons.ViewModels.CompositeViewModel
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")"></script>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Article</legend>
<div class="editor-label">
@Html.LabelFor(model => model.ArticleViewModel.Article.ArticleTitle)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ArticleViewModel.Article.ArticleTitle)
@Html.ValidationMessageFor(model => model.ArticleViewModel.Article.ArticleTitle)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.ArticleViewModel.Article.ArticleDate)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ArticleViewModel.Article.ArticleDate)
@Html.ValidationMessageFor(model => model.ArticleViewModel.Article.ArticleDate)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.ArticleViewModel.Article.ArticleText)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ArticleViewModel.Article.ArticleText)
@Html.ValidationMessageFor(model => model.ArticleViewModel.Article.ArticleText)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.ArticleViewModel.Article.ArticleSource)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ArticleViewModel.Article.ArticleSource)
@Html.ValidationMessageFor(model => model.ArticleViewModel.Article.ArticleSource)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.ArticleViewModel.Article.CategoryID, "Category")
</div>
<div class="editor-field">
@Html.DropDownList("CategoryID", String.Empty)
@Html.ValidationMessageFor(model => model.ArticleViewModel.Article.CategoryID)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
コントローラーには次のものがあります:-
[HttpPost]
public ActionResult Create(CompositeViewModel viewModel)
{
if (ModelState.IsValid)
{
unitOfWork.ArticleRepository.Insert(viewModel.ArticleViewModel.Article);
unitOfWork.Save();
return RedirectToAction("Index");
}
PopulateDropDownList(viewModel.ArticleViewModel.Article.CategoryID);
return View(viewModel);
}
CompositeViewModel は次のとおりです。
public class CompositeViewModel
{
public ImageViewModel ImageViewModel;
public ArticleViewModel ArticleViewModel;
public ProjectViewModel ProjectViewModel;
public CompositeViewModel()
{
ImageViewModel = new ImageViewModel();
ArticleViewModel = new ArticleViewModel();
ProjectViewModel = new ProjectViewModel();
}
}
私の問題は、[作成] リンクをクリックしてコントローラーのアクションに入ると、CompositeViewModel ビューモデルが空になることです。
誰でも理由を教えてもらえますか、理由がわかりません。
あなたの助けと時間をありがとう