1

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

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

18 Comments

its showing me the fields for the Project but its not showing the fields for each Task!
Ah, so the collection of tasks needs to be specified... give me a couple minutes..
So I dont have to do any kind of foreach?
No, the EditorForModel should just figure it out... I have done it with a custom view for the items in the collection, but I don't think the custom view is needed, and I didn't have to enumerate the collection.
weird.. I cant get it to work... after adding the editor for the collection its just displaying some GUID instead of showing me textboxes... I had to use EditorFor instead of EditorForModel btw
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.