简体   繁体   English

基于自定义模型创建强类型视图

[英]Creating a strongly typed view based on a custom model

I have need of a view that combines two entity models. 我需要一个结合了两个实体模型的视图。 I created a class that looks like this: 我创建了一个看起来像这样的类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FSDS.DataModels;

namespace FSDS.WebUX.Models
{
    public partial class ChainandJob
    {
        public ScheduleJobChain chain {get;set;} //this object has 6 properties
        public ScheduleJob job {get;set;} //this object has 8 properties.
    }
}

I created a new partial view using the "create" scaffolding. 我使用“创建”支架创建了一个新的局部视图。 This is what it gives me: 这就是给我的:

@model FSDS.WebUX.Models.ChainandJob

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>ChainandJob</legend>

        <p>
             <input type="submit" value="Create" />
        </p>
    </fieldset>
 }

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

Where are all the fields? 所有字段都在哪里?

You will need to write it yourself, something like: 您将需要自己编写它,例如:

@Html.EditorFor(model => model.ChainandJob.chain.Bla)
@Html.ValidationMessageFor(model => model.ChainandJob.chain.Bla)
@Html.EditorFor(model => model.ChainandJob.job.Bla)
@Html.ValidationMessageFor(model => model.ChainandJob.job.Bla)

And so on. 等等。

Edit 编辑

Although I don't do it this way, you could have VS create a edit view for SchedualJobChain , and another one for ScheduleJob and cut the templates it creates into one for you view model. 尽管我不是这样做的,但是您可以让VS为SchedualJobChain创建一个编辑视图,为ScheduleJob创建另一个视图,并将其创建的模板切成一个供您查看模型的模板。 Don't forget the @Html.HiddenFor(model => model.ChainandJob.chain.Id etc. 不要忘记@Html.HiddenFor(model => model.ChainandJob.chain.Id等。

VS doesn't know your objects. VS不了解您的对象。 Right after <legend>ChainandJob</legend> try adding <legend>ChainandJob</legend>尝试添加

@EditorFor(m => m.chain.ChainProperty)
@ValidationMessageFor(m => m.chain.ChainProperty)

@EditorFor(m => m.job.JobProperty)
@ValidationMessageFor(m => m.job.JobProperty)

and things will be hunky dory :) EditorFor will generate the default output for you, should you need to tweak it - feel free to do so. 事情将变得很笨拙:) EditorFor将为您生成默认输出,如果您需要对其进行调整,请随时执行。

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

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