简体   繁体   中英

Acessing Navigation Class properties in MVC4 View

My Class is

public partial class Team
    {
        public Team()
        {
            this.Trials = new HashSet<Trials>();
            this.Users = new HashSet<Users>();
        }

        public int TeamId { get; set; }
        public string TeamName { get; set; }
        public string TeamDescription { get; set; }
        public virtual ICollection<Trials> Trials { get; set; }
    }

and in the view I am trying to accessthe attirbutes of Trial class like

@model Project.Classes.Team

 <td>
         @for (int i = 0; i < Model.Trials.Count; i++ )
         {                
                @Html.DisplayFor(x => Model.Trials.ElementAt(i).Name)
                @Html.HiddenFor(x => Model.Trials.ElementAt(i).Name)
                @Html.HiddenFor(x => Model.Trials.ElementAt(i).TrialID)
                @Html.CheckBoxFor(x => Model.Trials.ElementAt(i).isChk)  
             <br />               
         } 
</td>

When view is displayed it shows the Trials objects in the model and displays on the form but when form is posted then in the controller I dont get any value for Trials it shows 0 Trials...I think I am doing some thing wrong in the binding loop ...

Any help appreciated......

Got it working by changing the Team calss. Instead using

 public virtual ICollection<Trials> Trials { get; set; }

I changed it to IList

public virtual IList<Trials> Trials { get; set; }

and commented the

 //this.Trials = new HashSet<Trials>();

and in the View

 <td>
         @for(int i=0; i<Model.Trials.Count; i++)
         {

                @Html.DisplayFor(x =>  x.Trials[i].Name)
                @Html.HiddenFor(x => x.Trials[i].Name)
                @Html.HiddenFor(x => x.Trials[i].TrialID)
                @Html.CheckBoxFor(x => x.Trials[i].isChk)  
             <br />

         }      

                </td>

Now I am getting the true model binging value for Team and Trials inside the team........

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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