0

I would like to have an empty table on my View, and fill it using AJAX based on some dropdown list selection by the user. My model contains data for the dropdown list. As table header is not changing, I want it to be statically on the View. I wonder what is a good way to pass to the View column names to be used in @Html.DisplayNameFor(). Of course, I can pass one record and retrieve the name from it, e.g.

@Html.DisplayNameFor(model => model.Employees[0].LastName)

But it seems to me awkward. I would like to avoid sending data to the View, as the table initially is empty, so the data itself is not used. Could you please suggest a more elegant way? Thanks.

  • Thank you for your answer. It seems to me though that it is not quite the same. In that question the data itself was passed to the view, and that is what I would like to avoid, if possible. – Dmytro Reznik Aug 25 '15 at 21:48
  • That's exactly the same question. The suggested solution is creating a custom html helper. However, I don't think it's necessary to create an html helper for such a simple thing. What you have in your question works. – ataravati Aug 25 '15 at 21:51
  • I know it works, but I want to avoid sending to my View data that is not needed. All I need is column names. – Dmytro Reznik Aug 25 '15 at 21:57
  • What do you mean? What do you want to avoid sending to View data? – ataravati Aug 25 '15 at 21:58
  • As I mentioned in my question, the table of e.g. Employees is initially empty. Only its header is present. So it seems to me strange to send a record of data only to get the column names from it. – Dmytro Reznik Aug 25 '15 at 22:00
  • You don't need data to get the name. Did you even look at the answer in the second link I posted here? – ataravati Aug 26 '15 at 01:00
  • @ataravati Hmm, I see only one link at the top of the discussion. – Dmytro Reznik Aug 26 '15 at 18:40

2 Answers2

1

It's a lot more straight-forward than that, actually. MVC's pretty clever about this, the following code will work even if there are no entries in your list:

@Html.DisplayNameFor(model => model.Employees.First().LastName)
Eraph
  • 1,019
  • 1
  • 10
  • 21
  • is it different than what I have in my answer? :) I don't even need to specify .First() – Dmytro Reznik Aug 25 '15 at 23:01
  • Slightly, yes. Because you're specifying an index in a list, it makes an attempt to evaluate that, so it'll fall over if you have an empty list. Using `First()`, MVC is clever enough to say "yeah, whatever, just gimme the name" – Eraph Aug 25 '15 at 23:11
  • you are talking about my original question, but I made an answer 9 minutes before you. :) You can see that I don't instantiate anything at all, and yet I have my metadata. And instead of the list of EmployeeViewModel, I have a member of type EmployeeViewModel that has all metadata I need. – Dmytro Reznik Aug 25 '15 at 23:25
  • Ah, I see what you mean. Yeah, it will all do the same thing, at this point it's just a matter of preference. – Eraph Aug 25 '15 at 23:27
0

I came to a solution that works, though I am not sure that it is the best solution possible. First, to my ViewModel (which is EmployeesViewModel) I added a member of type EmployeeViewModel:

public class EmployeeViewModel
{
        public int Id { get; set; }
        [Display(Name = "First Name")]
        public string FirstName { get; set; }
        [Display(Name = "Last Name")]
        public string LastName { get; set; }
        public string Gender { get; set; }
        public int? Salary { get; set; }
        public int? DepartmentId { get; set; }
        [Display(Name = "Department Name")]
        public string DepartmentName { get; set; }
}

public class EmployeesViewModel
{
    public SelectList Departments { get; set; }
    public EmployeeViewModel EmployeeColumnNamesRetriever { get; set; }
}

In the controller, I instantiate my Departments, but not EmployeeColumnNamesRetriever, as I need it only to get its metadata:

        EmployeesViewModel employees = new EmployeesViewModel
        {
            Departments = new SelectList(db.Departments, "Id", "Name"),
        };

And in the View:

    <th>
        @Html.DisplayNameFor(model => model.EmployeeColumnNamesRetriever.FirstName)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.EmployeeColumnNamesRetriever.LastName)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.EmployeeColumnNamesRetriever.Gender)
    </th>

EmployeeColumnNamesRetriever is null, but it does not matter. If somebody can suggest something nicer, I would be grateful.

  • It would be nice if the person that placed a negative mark to my answer could write what he (she) disliked, and maybe suggested something better. Otherwise it is not very helpful. – Dmytro Reznik Aug 26 '15 at 18:35