简体   繁体   中英

How to get the value of 1st row “Name” from partial view when page first loads in parent view?

I want to get the value of 1st row Name from partial view when page first loads in parent view. Please suggest me the way.

Model

public class ClsA
{        
    public List<first> firsts{ get; set; }    
    public List<second> seconds{ get; set; }
}

public class first
{     
    public string Name { get; set; }
    public string Address { get; set; }

}

public class second
{
    public string Details{ get; set; }
    public string Age{ get; set; }             
}

Controller

 public ActionResult ABC()
 {
   string name="abc";// currently I am passing value manually
   SDetails sDetails=new SDetails();        
   var model = new ClsA();
   model.firsts = sDetails.Rst();            
   model.seconds = sDetails.Rs(name);   //I want to pass name value from 1st row of table        
   return View(model);
 }

View

  @model Aplication.Models.ABC.ClsA 

      @{ Html.RenderPartial("_Partial", Model.firsts); }
      @{ Html.RenderPartial("_PartialB", Model.seconds);}   

PartialA

@model  IEnumerable<Aplication.Models.ABC.first>

    <table>
        <tr>
           <th>@Html.DisplayNameFor(m => m.Name)</th>
            <th>@Html.DisplayNameFor(m => m.Address)</th>            
        </tr>

        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.Name)
                </td>
                <td>                   
                    @Html.DisplayFor(modelItem => item.Address)                    
                </td>                

            </tr>
        }
    </table>

_PartialB

@model  IEnumerable<Aplication.Models.ABC.second>

    <table>
        <tr>
           <th>@Html.DisplayNameFor(m => m.Details)</th>
            <th>@Html.DisplayNameFor(m => m.Age)</th>            
        </tr>

        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.Details)
                </td>
                <td>                   
                    @Html.DisplayFor(modelItem => item.Age)                    
                </td>                

            </tr>
        }
    </table>

如果要从第一个局部视图中静态地选择第一行,可以在视图中尝试以下代码Model.firsts().FirstOrDefault().Name

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