简体   繁体   English

ASP.NET MVC编辑器的帮助

[英]Help with asp.net mvc EditorFor

I have a Project model and a Task model. 我有一个项目模型和一个任务模型。

say in my controller I have 说在我的控制器中我有

public ActionResult Create()
{
   Project p = new Project();
   Task t1=new Task();
   Task t2=new Task();
   Task t3=new Task();
   p.tasks.Add(t1);
   p.tasks.Add(t2);
   p.tasks.Add(t3);
}

and in my strongly typed View for Project I have 在我的项目的强类型视图中

Html.EditorForModel()

How can I make this show the fields for the Project and also for each task? 我该如何显示项目和每个任务的字段?

please help! 请帮忙!

-------Added for clarification -------为澄清而添加

public ActionResult Create(Guid LicenseId)
        {
            License license;
            if (!User.IsInRole("admin"))
                license = _service.GetContributor(User.Identity.Name).Licenses.Single(l => l.Id == LicenseId);
            else
                license = _service.GetLicenseById(LicenseId);
            if (license == null)
                return RedirectToAction("Index", "Home");
            IncomeDeclaration i = new IncomeDeclaration();
            foreach(var ecoActLicense in license.EconomicActivitiyLicenses)
            {
                EconomicActivityIncomeDeclaration ecoActIncDec = new EconomicActivityIncomeDeclaration();
                ecoActIncDec.ActivityTax = 20;
                ecoActIncDec.EconomicActivityId = ecoActLicense.EconomicActivityId;
                i.EconomicActivityIncomeDeclarations.Add(ecoActIncDec);
            }
            return View(i);
        }

This code is where I first relate the EconomicActivityIncomeDeclarations to the IncomeDeclaration. 该代码是我首先将EconomicActivityIncomeDeclarations与IncomeDeclaration联系起来的地方。

and in the View, which is strongly typed for IncomeDeclaration I simply have 在“视图”中,为“收入申报”强类型输入

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

        @Html.EditorForModel()
        @Html.EditorFor(m =>m.EconomicActivityIncomeDeclarations)
        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

Your view should be strongly typed to the Project as the model, and if it is it should mostly just work automatically. 您的视图应作为模型强力键入到Project中,如果是,则大多数情况下应该只是自动工作。

The public properties on Project (if it's the specified model for a strongly typed view), will be discovered and presented with Labels and textboxes for editing. 将发现Project上的公共属性(如果它是强类型视图的指定模型),并带有用于编辑的标签和文本框。

If you want to show the editor for a property that is a collection you could do something like: 如果要显示作为集合的属性的编辑器,则可以执行以下操作:

Html.EditorForModel(model => model.tasks)

If necessary you can add an EditorTemplate for your collection item: Views-->Shared-->EditorTemplates-->Task.cshtml . 如有必要,可以为您的集合项添加一个EditorTemplate: Views-->Shared-->EditorTemplates-->Task.cshtml

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

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