0

A quick question:

So, I am developing a small MVC/C# application and I am using ViewModel to pass data to my view. The ViewModel is actually a combination of 3 Models.

My Models:

public class Sourcing_ProfileSourcingAreas
{
    public string Area { get; set; }

    public string Details { get; set; }
}


public class DefCountry
{
    public string CountryName { get; set; }

    public string CountryCode { get; set; }
}

My ViewModel:

public class SourcingIndex
{
    public List<DefCountry> CountryLst { get; set; }

    public List<Sourcing_ProfileSourcingAreas> AreaLst { get; set; }
}

On my view, I put this line at the top @model SourcingIndex to declare that I will be using this ViewModel

I also was easily able to specify which model I want to display by using foreach loop, for example:

@foreach (Sourcing_ProfileSourcingAreas area in Model.AreaLst)
    {
          <tr>
              <td>@area.Area</td>
              <td>@area.Details</td>                  
          </tr>           
    }

And

@foreach (DefCountry ctry in Model.CountryLst)
{
    <option>@ctry.CountryName</option>
}

However, I am not able to create @Html.TextBox and assign it to a specific property in a model!

If possible, I want it to be somthing like this: @Html.TextBoxFor(model => model.AreaLst.Area)

Thank you

Ahmad Shli
  • 69
  • 2
  • 10
  • 2
    the trick to doing this is to use a `for` loop not a `foreach` loop and then access the List using an indexer `[0]`, `[1]`, etc. – Liam Sep 12 '17 at 14:55
  • I'm not sure what "I am not able to create" . Is there a run-time error? It could be something to do with Lazy-Loading vs not using .Include() for EF in your controller code. – AlignedDev Sep 12 '17 at 14:57
  • I see, it seem that I need the foreach loop in order to achieve my goal – Ahmad Shli Sep 12 '17 at 15:28
  • Refer [this answer](http://stackoverflow.com/questions/30094047/html-table-to-ado-net-datatable/30094943#30094943) for how to use a `for` loop or `EditorTemplate` to generate form controls for a collection. But since you editing data, do not use data models inside a view model - create view models for each collection object –  Sep 12 '17 at 22:44
  • @StephenMuecke would you please give me an example? Thanks – Ahmad Shli Sep 13 '17 at 12:15
  • My previous comment includes a link with examples –  Sep 13 '17 at 12:17
  • I will look at the link. Thank you! – Ahmad Shli Sep 13 '17 at 16:03

2 Answers2

1

It's a bit of a weird quirk about Razor. If you try and access the objects in a foreach loop it struggles to resolve where in the model it is. You need to use this syntax:

@for (int x = 0; x < Model.CountryLst.Count(); x++)
{
    @Html.TextBoxFor(t => t.CountryLst[x].CountryName)
}

this should produce an input something like

<input name="countryLst[1].CountryName"/>
Liam
  • 27,717
  • 28
  • 128
  • 190
1
@for(var i = 0; i < Model.CountryLst.Count(); i++)
{
    <text>@Html.TextBoxFor(p=>Model.CountryLst[i].CountryCode)</text>
}
GGO
  • 2,678
  • 4
  • 20
  • 42