0

I am working with an MVC4 application. I have created the model class and its child collections.

And since I am developing a strongly typed view using the .cshtml page which takes the parent model class as its model, in the .cshtml page, I don't know how can I access the attributes of the child collection. When I say something like,

@Html.TextBoxFor(model=>model.)

It lists only the parent's direct attributes and I don't know how to access the child collections attributes in this .cshtml page.

My model:

    public partial class Parentmodel
       {
        public string FirstName;
        public string LastName;

        public ICollection<ChildEntity> ChildEntityObj { get; set; }
       }

    public class ChildEntity
       {
        public decimal PhoneNumber;
        public string Addr1;
        public string CountryNowLiving;
       }

In the view, I am using the ParentModel. So, I am not able to access the PhoneNumber in my .cshtml file. Not only PhoneNumber, But I have many fields in the ChildEntityCollection. Only first name and last name comes up in the dropdown. I understand that I must use the foreach or for loop but I am not sure of how it should be proceeded.

halfer
  • 19,824
  • 17
  • 99
  • 186
user2771399
  • 135
  • 1
  • 4
  • 15

2 Answers2

0

Since your "child" object is a ICollection you should be able to iterate over it

@foreach(var child in Model.ChildEntityObj) {
    Html.TextBoxFor(x => child.MemberNameGoesHere);
}

If you have nested collection, keep stacking foreach blocks (no need to prepend a @ in nested statements, VS will complain about it so not really a issue, it won't build).

The following approach applies to any member of your model (decimal or string)

@foreach(var child in Model.ChildEntityObj) {
    // Here you basically have an object named "child" which is one
    // of the elements of your collection, as is usual in a foreach loop

    Html.LabelFor(x => child.PhoneNumber); // While we're at it, give it a label
    Html.TextBoxFor(x => child.PhoneNumber);

    // Same for other members
}

LabelFor would show the member name ("PhoneNumber" in this case) unless you add a DisplayNameAttribute to the member:

using System.ComponentModel.DataAnnotations;
public class ChildEntity
{
    [Display(Name="Phone Number")]
    public decimal PhoneNumber;
}

This may of course be made fancier (through a custom-tailored EditorTemplate to enforce constraints on the input i.e. prefixes and the sort) but this is the most basic implementation.

Alex
  • 23,004
  • 4
  • 39
  • 73
  • Hi @Alex, Should I use this foreach loop over every member of my ChildCollection? Or all the members within this foreach loop? sorry for my ignorance... – user2771399 Dec 13 '13 at 08:40
  • I amended the answer to account for the actual model structure – Alex Dec 13 '13 at 08:47
  • what I meant was is what I have shown edited . It does not have only PhoneNumber. Now can I put all the attributes within one foreach loop or one foreach loop for each attribute? – user2771399 Dec 13 '13 at 08:50
  • I re-amended the code. One foreach is enough, the `child` object contains all the members as per normal `foreach` behavior. – Alex Dec 13 '13 at 08:52
  • But i wasnt able to access the childEntity object directly. It was Model.ChildEntityObj. Thanks a much for your code. It helped a lot. – user2771399 Dec 13 '13 at 09:16
  • Meh you're right I mistyped it! Re-re-amended to fix the code – Alex Dec 13 '13 at 09:48
  • Hi Alex, when i use the above foreach(), i get the attributes of the child get repeated in the display also. what can i do for that? – user2771399 Dec 16 '13 at 10:39
  • To receive better answers, you should make that a new question focused on the fact that data is repeated. In the new question, remember to include the relevant code parts, also detail what you expect to see and what you see instead. Also include a link to this question as a reference. – Alex Dec 16 '13 at 12:24
-1

foreach loop is enough for data showing purposes but if you post the data back to controller action then posted collection values may be come to controller actions so you can use for loop for this purpose

for(int i=0;i< Model.ChildEntityObj.count;i++)
{
  Html.TextBoxFor(x => x.ChildEntityObj[i].ChildMember);
}

Hope this will helps you for future purposes

unique
  • 5,442
  • 6
  • 23
  • 26
  • This is the same as using `foreach` – Alex Dec 16 '13 at 12:20
  • @Alex But try to post data to controller action using foreach loop it is not working but that is working for "for" loop that is defference – unique Dec 16 '13 at 15:25
  • I do, all the time., never had problems. I believe that a piece of code which shows this kind of issue would make for a good question though. – Alex Dec 16 '13 at 15:55
  • @Alex See these questions http://stackoverflow.com/questions/15636284/for-each-loop-with-controls-and-submitting-form-html-beginform-in-mvc-4 and this http://stackoverflow.com/questions/14165632/asp-net-mvc-4-for-loop-posts-model-collection-properties-but-foreach-does-not I am also had same problem finally fixed this issue using for loop – unique Dec 17 '13 at 04:19