0

Confused here. Just want to use the sample data in the constructor to display one field and then loop through the one record :

public class MikesViewModel
{
    public MikesClass MikesClass { get; set; }

    public List<MikesClass> MikesClassList { get; set; }
}

public ActionResult MikesView()
{   
    var viewClassModel = new MikesViewModel();

    viewClassModel.MikesClass = new MikesClass();

    viewClassModel.MikesClassList = new List<MikesClass> { new MikesClass() {} };

    return View(viewClassModel);    
}

View is the problem : the foreach won't work if I take out @Html.DropDownListFor :

@using AAA.Models    
@model MikesViewModel  


@Html.EditorFor(Model => Model.MikesClass.Name) 


@using (Html.BeginForm())
{

    @Html.DropDownListFor(model => model.MikesClass.Id, 
                          new SelectList(Model.MikesClassList, "Id", "Name"))

}

**** This foreach won't work if I take out the ^ above line ** :**

@foreach (var item in Model.MikesClassList)
{

@item.Name
}

The weird thing is, if I take out the :

@Html.DropDownListFor(model => model.MikesClass.Id, new SelectList(Model.MikesClassList, "Id", "Name"))

the @foreach (var item in Model.MikesClassList) doesn't work and gives the error :

'Model' conflicts with the declaration 'System.Web.Mvc.WebViewPage<TModel>.Model'

How can I get the foreach to work? Not understanding this.

Flat Eric
  • 7,971
  • 9
  • 36
  • 45
Mike Howe
  • 263
  • 2
  • 8
  • 19
  • See: http://stackoverflow.com/questions/6204301/compiler-error-message-cs0135-model-conflicts-with-the-declaration-system-w) – brk Jul 06 '14 at 15:39
  • I actually saw that answer before I posted mine, but I guess I didn't understand what was going on yet, . . but yes, it was the same problem I had. – Mike Howe Jul 06 '14 at 19:38

1 Answers1

1

Don't call things "Model" or "model" in the view. That's probably confusing the view engine. Lines like this, for example:

@Html.EditorFor(Model => Model.MikesClass.Name)

You're declaring a variable in EditorFor called Model, but there's already a built-in variable in the view engine called Model. When you dereference it as Model.MikesClass which one are you referring to? You have another one called model here:

@Html.DropDownListFor(model => model.MikesClass.Id, new SelectList(Model.MikesClassList, "Id", "Name"))

While Model and model aren't reserved words in C#, it's best to consider them "reserved" in views. Basically, it's best to use meaningful names for your variables and not overly-generic ones.

foreach works just fine, but other lines in that view have already confused the engine that it doesn't know what to do when it gets to the foreach.

David
  • 208,112
  • 36
  • 198
  • 279
  • Ah, thanks. I didn't even realize I was using a variable there (I guess I copied that from an example somewhere that had : (Model => Model...). If I use (r => r.MikesClass.Name) the foreach works. This lambda stuff can get confusing but provides easy coding. – Mike Howe Jul 06 '14 at 16:27