Asp.Net Mvc 4从viewmodel生成视图

时间:2014-02-24 07:52:04

标签: c# asp.net-mvc asp.net-mvc-4

我正在尝试为viewmodel创建一个编辑页面。该视图仅显示视图模型中字符串的控件。如何让表单显示其他对象的控件?如果我在viewmodel中手动添加Resource模型的控件,则表单将资源发布为null。

viewmodel

public class ViewModelResourceReturn
{
    public string Teststring { get; set; }

    public Resource Resource { get; set; }

    public ViewModelResult Result { get; set; }

    public List<SelectListItem> RoleCheckboxes { get; set; }

}

控制器

  [HttpGet]
    public ActionResult AddEditRecord(int? id)
    {
        ResourceRestApi api = new ResourceRestApi(this);
        if (id != null)
        {
            ViewBag.IsUpdate = true;
            ViewModelResourceReturn resource = api.GetResource(id ?? 0);
            return PartialView(resource);
        }
        ViewBag.IsUpdate = false;
        return PartialView();
    }

生成的视图

@model Project.ViewModels.ResourceViewModels.ViewModelResourceReturn

<script src="~/Scripts/jquery-1.8.2.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>

@using (Html.BeginForm()) {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>ViewModelResourceReturn</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.Teststring)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Teststring)
            @Html.ValidationMessageFor(model => model.Teststring)
        </div>
        @Html.EditorFor(m => m.Resource)
        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

编辑模板:

@model Project.Models.Resource

@{
    Layout = null;
}


            @Html.HiddenFor(model => model.ResourceId)

            <div class="editor-label">
                @Html.LabelFor(model => model.EmailAddress)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.EmailAddress)
                @Html.ValidationMessageFor(model => model.EmailAddress)
            </div>

            <div class="editor-label">
                @Html.LabelFor(model => model.Password)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.Password)
                @Html.ValidationMessageFor(model => model.Password)
            </div>

            <div class="editor-label">
                @Html.LabelFor(model => model.FullName)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.FullName)
                @Html.ValidationMessageFor(model => model.FullName)
            </div>

            <div class="editor-label">
                @Html.LabelFor(model => model.TimeManagerResourceId)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.TimeManagerResourceId)
                @Html.ValidationMessageFor(model => model.TimeManagerResourceId)
            </div>

            <div class="editor-label">
                @Html.LabelFor(model => model.TravelManagerResourceId)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.TravelManagerResourceId)
                @Html.ValidationMessageFor(model => model.TravelManagerResourceId)
            </div>

            <div class="editor-label">
                @Html.LabelFor(model => model.OvertimeManagerResourceId)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.OvertimeManagerResourceId)
                @Html.ValidationMessageFor(model => model.OvertimeManagerResourceId)
            </div>

            <div class="editor-label">
                @Html.LabelFor(model => model.AbsenceManagerResourceId)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.AbsenceManagerResourceId)
                @Html.ValidationMessageFor(model => model.AbsenceManagerResourceId)
            </div>

编辑:

    [HttpPost]
    public ActionResult AddEditRecord(ViewModelResourceReturn resource)
    {
        return PartialView(resource);
    }

1 个答案:

答案 0 :(得分:1)

您必须为其他属性创建编辑器。它无法猜测如何渲染复杂的。

您可以使用@Html.EditorFor(m => m.Resource)并为Resource类型

创建编辑器模板

编辑:在POST操作中,尝试将参数重命名为resource以外的其他值。这可能会导致模型绑定器出现一些恐慌。