Вопрос

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.

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.

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.

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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top