2

我有一个创建视图:-

@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();
    }
}

我的问题是,当我单击 Create 链接并进入 Controller 的 Action 时,CompositeViewModel viewModel 是空的。

谁能告诉我为什么,我似乎无法弄清楚为什么。

感谢您的帮助和时间

4

1 回答 1

3

在模型中将现有字段设置为属性并且不要使用构造函数,因为在每个模型实例化时它将创建新成员:示例:

 public class CompositeViewModel
 {
    public ImageViewModel ImageViewModel { get; set; };
    public ArticleViewModel ArticleViewModel{ get; set; };
    public ProjectViewModel ProjectViewModel{ get; set; };
 }
于 2012-08-23T07:11:55.260 回答