1

I am new in MVC and learning MVC. Now I do not want to use any grid extension, rather I want to generate tabular UI just by HTML table. So I was looking for code and found a hint.

This code is not full code. Here is what I got.

<% if (Model.Count() > 0)
{ %>
    <table width="35%">
 <thead><tr><th>Species</th><th>Length</th></tr></thead>
  <%  foreach (var item in Model)
    { %>
     <tr>
         <td><%= item.FishSpecies%></td>
         <td align="center"><%=item.Length%></td>
     </tr>

 <% } %>
    </table>
<% }
else
{ %>
No fish collected.
<%} %>

The problem is I am not being able to visualize how the model class should look like and how it is populated from controller. Viewbag is not used in code, rather generating table directly from model class.

So can anyone write a small complete code for me just to create a HTML table and populate with model directly without using viewbag?

Need code for model, controller and view too.

2
  • Which version of MVC are you using? Commented Jul 25, 2013 at 11:46
  • Oh really. First, this is not a complete code. What were your efforts to write a controller and action? And model? Commented Jul 25, 2013 at 11:47

2 Answers 2

1

Your model actually needs to be a IEnumerable<Model>. So you might have a model:

public class Model
{
    public string FishSpecies { get; set; }
    public int Length { get; set; }

    public static IEnumerable<Model> Load() { ... }
}

and then in your controller's action:

var list = Model.Load();
return View(list);

and then in the view you need to define the model at the very top:

@model System.Collections.IEnumerable<My.Namespace.Model>

Now, these two lines aren't going to work:

<td><%= item.FishSpecies%></td>
<td align="center"><%=item.Length%></td>

they need to be something more like this:

<td>@Html.DisplayFor(m => item.FishSpecies)</td>
<td>@Html.DisplayFor(m => item.Length)</td>
Sign up to request clarification or add additional context in comments.

4 Comments

what is item ? it is not define in code. just see this code <td>@Html.DisplayFor(m => item.Length)</td> here u use lambda so here u use m but u actually use item.Length instead of m.Length? anyway i am not good in mvc so looking for ur comment against my answer. thanks
if we can directly populate model from controller and use that model in view to populate any control then why should one use viewbag ? any idea when job can be accomplish directly using model then why viewbag need to use.
@Thomas, to answer your first question, the DisplayFor requires a Func, so I have to send in m (i.e. the name of the parameter), however, because you're in a loop I really need to reference the value of item. That's what this is doing. It's meeting the needs of the method, but using lexical scoping to grab the value from item.
@Thomas, the ViewBag is rarely needed if you're working with models. A good example is you might use the ViewBag for a title that can be set in the controller.
0

If you want to iterate through your model and create your table, first of all change the @model of your view like this:

@model IEnumerable<myPrj.Models.EntityName>

Then, you should change your action method to supply model items to your view:

public ActionResult Index()
{            
    return View(db.EntityNames.ToList());
}

and finally, iterate your model and create your table:

<table id="tblNews">
<tr>
    <th>
        @Html.DisplayNameFor(model => model.Property1)
    </th>

    // ...

    <th>
        @Html.DisplayNameFor(model => model.Propertyn)
    </th>        
</tr>

@foreach (var item in Model) {
<tr>
    <td>
        @Html.DisplayFor(modelItem => item.Property1)
    </td>

    // ... 

    <td>
        @Html.DisplayFor(modelItem => item.Propertyn)
    </td>        
</tr>
}
</table>

and about the model? Model is nothing but a class with some properties:

public class MyModel
{
    [Display(Name = "Property 1")]
    [Required(ErrorMessage = "Property cannot be empty")]
    public int Property1 { get; set; }

    // ...

    [Display(Name = "Property n")]
    [Required(ErrorMessage = "Property cannot be empty")]
    public string Propertyn { get; set; }        
}

6 Comments

if we can directly populate model from controller and use that model in view to populate any control then why should one use viewbag ? any idea when job can be accomplish directly using model then why viewbag need to use.
tell me about DisplayNameFor & DisplayFor helper method. what kind of html tag it generate ? does it generate label or <TD> ?
I myself doesn't use viewbag at all, but to send some very simple data such as a flag or a little string to the view. Another common use of viewbag is to send a SelectList collection to view to populate DropDownList.
To understand what @Html.DisplayNameFor and @Html.DisplayFor do, first take a look at the model MyModel in my answer. The first one renders value of the first attribute [Display(Name = "Property 1")] which is Property 1. And the second renders the value of that property which your model brings to the view. I don't know exactly what codes are generated, however I know these will be <label> tags with some attributes.
DisplayNameFor is a helper that will give you the display name of a property if one has been given via attribute decoration or the property name itself as an HTML label, I believe. DisplayFor works similarly with the value of the property but it also checks first to see if you have any DisplayTemplates to use for that property type (custom render template rather than default HTML).
|

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.