Click here to Skip to main content
15,886,664 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to access model name using multiple view as a parent model. The problem now i want to access them one by one as TextBoxFor(model=>model.having its name not property object name)

What I have tried:

// Parent Model Name
public List<Incident_NameDropDown> NameList { get; set; }


// Model Name called
public class Incident_NameDropDown
  {
      public string Title { get; set; }


      public string Description { get; set; }
  }


// View to access model name
<fieldset>
    @using (Html.BeginForm("Index", "Home", FormMethod.Post))
    {
    <div class="editor-label">

        @Html.LabelFor(model => model.NameList)// This prints name like Title now it prints NameList and this is wrong and confusing

    </div>


    <div class="editor-field">
        @Html.TextBoxFor(model=>model.NameList)
    </div>
    }
</fieldset>
Posted
Updated 8-Nov-21 23:20pm

1 solution

Iterate through the list and display a field for each item.
Razor
@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
    for (int index = 0; index < NameList.Count; index++)
    {
        <div class="editor-label">
            @Html.LabelFor(m => m.NameList[index].Description, Model.NameList[index].Title)
        </div>
        <div class="editor-field">
            @Html.EditorFor(m => m.NameList[index].Description)
        </div>
    }
}
 
Share this answer
 
Comments
Gcobani Mkontwana 2021 9-Nov-21 7:53am    
@Richard thanks, Namelist when i used this code is complaining that it does not exist in current context. Inside the loop itself, do i need to change that it seems its outside the scope.
Richard Deeming 9-Nov-21 10:00am    
Complaining that what does not exist?

I think you need to go back to the start, and explain what your @model type is, and precisely what you are trying to do.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900