简体   繁体   English

Asp.Net Mvc 4从ViewModel生成视图

[英]Asp.Net Mvc 4 Generate view from viewmodel

I am trying to create a edit page for a viewmodel. 我正在尝试为视图模型创建一个编辑页面。 The view only shows a control for the string in the view model. 视图仅显示视图模型中字符串的控件。 How do I get the form to display controls for the other objects? 如何获取表单以显示其他对象的控件? If I manually add controls for the Resource model in the viewmodel the form post the resource as null. 如果我在视图模型中手动为Resource模型添加控件,则表单将资源发布为null。

The 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; }

}

The controller 控制器

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

The view that is generated 生成的视图

@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>

Editor Template: 编辑器模板:

@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>

Edit: 编辑:

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

You have to create the editors for the other properties. 您必须为其他属性创建编辑器。 It cannot guess how to render the complex ones. 它无法猜测如何渲染复杂的图像。

You can use @Html.EditorFor(m => m.Resource) and create an editor template for the Resource type 您可以使用@Html.EditorFor(m => m.Resource)并为Resource类型创建一个编辑器模板

EDIT: In the POST action, try renaming the parameter to something other than resource . 编辑:在POST操作中,尝试将参数重命名为resource以外的名称。 It may cause the model binder to panic a little. 这可能会导致模型绑定程序有些恐慌。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM