0

View:

@model IEnumerable<models.AppModel> 
@using (Html.BeginForm()){
    <table>
    <tr>
        <td>
            @Html.LabelFor(model => model.Age) //error
</td>
</tr>
</table>
}

Controller:

public ActionResult Index(){
    if (string.IsNullOrWhiteSpace(unitCode))
            {
                return View(entity.dBSet.ToList());
            }
    else // do something else
}

Model:

namespace models{
    public class AppModel{
        public short Age {get; set;}
        public string Nationality {get; set;}
    }
}

In the view, every property of the model class such as Model.Age, Model.Nationality has this error. How do I fix it?

Dylan Czenski
  • 1,305
  • 4
  • 29
  • 49
  • Is this just for the header row of your table? If so, you can use `@Html.DisplayNameFor(m => m.Age)`. Otherwise you need a `for` loop of `EditorTemplate` as per [this answer](http://stackoverflow.com/questions/30094047/html-table-to-ado-net-datatable/30094943#30094943) to generate the form controls –  Jun 02 '16 at 22:11
  • @StephenMuecke Thank you. It is not a table header, it is a form and every `td` contains a Label+textbox or Label+dropdown list combo like this http://imgur.com/v7MEN1e – Dylan Czenski Jun 02 '16 at 22:23
  • Your image makes no sense. You have a collection so its either a form with labels/controls for each item in the collection to edit all values (and post back the collection - see the link in my previous comment), or you have one object so its `@model models.AppModel` –  Jun 02 '16 at 22:38
  • And if it is a form for editing a single `AppModel` then do not use `` elements. Tables are for tabular data, not for layout.
    –  Jun 02 '16 at 22:40
  • Thank you for the advice. Also the what layour should I use for the form? the `` actually is looking not bad.
    – Dylan Czenski Jun 02 '16 at 22:47
  • Using Bootstrap is the most common way for layout of forms (also recommend you read [these answers](http://stackoverflow.com/questions/83073/why-not-use-tables-for-layout-in-html) and [numerous others](https://www.google.com.au/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=html%20tables%20for%20layout%20bad) explaining why tables should not be used for layout –  Jun 02 '16 at 22:52

0 Answers0