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

7
  • 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. Commented Sep 12, 2017 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. Commented Sep 12, 2017 at 14:57
  • I see, it seem that I need the foreach loop in order to achieve my goal Commented Sep 12, 2017 at 15:28
  • Refer this answer 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 Commented Sep 12, 2017 at 22:44
  • @StephenMuecke would you please give me an example? Thanks Commented Sep 13, 2017 at 12:15

2 Answers 2

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"/>
Sign up to request clarification or add additional context in comments.

Comments

1
@for(var i = 0; i < Model.CountryLst.Count(); i++)
{
    <text>@Html.TextBoxFor(p=>Model.CountryLst[i].CountryCode)</text>
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.