1

I posted a question earlier which was marked a duplicate since it is somewhat similar to an existing answered-question. In my original question question I explained the problem that I had.

Now, I am trying to use my take away from the question that has the answer "i.e.Post an HTML Table to ADO.NET DataTable" and trying to implement it. The main different between what I am doing and what the answer have is that I encapsulate multiple ViewModels in a presentation class because the view has multiple forms. Additionally, I use use DropDownListFor which requires a SelectItem list that depends on another property in my presenter class.

Here is what I have done

I created a template view for my grid rows like this

@model Proj.ViewModels.UserProfileAccessUserToClientsViewModel
<tr>
    <td>
        @Html.HiddenFor(m => m.ClientId)
    </td>
    <td>

        Menu Goes here -- Still to do since I don't know how to pass SelectItem to this view

    </td>
    <td>
        @Html.TextBoxFor(m => m.JoinedAt, new { @class = "form-control date-picker small-input" })
    </td>

    <td>
        @Html.RadioButtonFor(m => m.IsActive, true) Allowed
    </td>

    <td>
        @Html.RadioButtonFor(m => m.IsActive, true) Not Allowed
    </td>

</tr>

Then in one of the form where I want to display the table, I do this

@model Proj.Presenters.UserProfilePresenter

<div class="panel panel-default">

    <div class="panel-heading">User To Client Association</div>

    <table class="table table-condensed table-valign-text" id="UserToClientRelationTable">

        <thead>
            <tr>
                <th>Client</th>
                <th>Default Team</th>
                <th class="text-center">Join At</th>
                <th colspan="2" class="text-center">Status</th>
            </tr>
        </thead>

        <tbody>
            @Html.EditorFor(m => m.Access.UserToClients)
        </tbody>

        <tfoot>
            <tr>
                <td colspan="5" class="text-center">
                    <ul class="pagination pagination-sm pager" id="UserToClientRelationTablePager"></ul>
                </td>
            </tr>
        </tfoot>
    </table>

</div>

Now, when I run my code there are no rows for my tables. Perhaps, I am not including my template in my main view. But also not sure how would I do that.

Questions

How can I correctly use my edit template to display the rows?

How can I pass a variable to my edit template which is needed to generate the SelectItem list for the DropDownList?

Community
  • 1
  • 1
Junior
  • 11,602
  • 27
  • 106
  • 212
  • Is the template named `UserProfileAccessUserToClientsViewModel.cshtml` an is it located in the `/Views/Shared/EditorTemplates` folder? For the dropdownlist, you need to pass it using `additionalVIewData` (will add a link shortly) –  Sep 27 '16 at 23:45
  • Refer Option 2 in [this answer](http://stackoverflow.com/questions/26162218/editortemplate-for-dropdownlist/26417466#26417466). And another one [here](http://stackoverflow.com/questions/31174250/how-to-set-default-value-to-select-list-in-mvc-during-run-time/31174308#31174308) –  Sep 27 '16 at 23:49
  • to answer your question no. I did not know the name and the location matters here. I have created a folder called EditorTemplates in the Shared folder and created a view with the exact name you mentioned. but still now showing anything – Junior Sep 27 '16 at 23:51
  • Do you have items in your collection? (if the file is named correctly, and is in the correct folder, the code you have shown will work fine) –  Sep 27 '16 at 23:54
  • As a side note, your 2nd radio button should be `@Html.RadioButtonFor(m => m.IsActive, false)` (not `true`) –  Sep 27 '16 at 23:56
  • I corrected the radio button. Additionally, the list was emply which is why I wasn't seeing data. Now that I corrected that problem, but this leads me to a new issue. The template/rows is being generated without the ..... markup. the template is generating plain text only – Junior Sep 28 '16 at 00:14
  • Your markup looks fine so should not be doing that. Are you sure its actually using your `EditorTemplate` (put a breakpoint inside the template to check its being hit) when the main view is rendered. –  Sep 28 '16 at 00:19
  • yes. I just put the break point and I do reach it. I just can't tell if the `@Html.EditorFor(m => m.Access.UserToClients)` is calling the correct template or not. I am assuming it is because that is the only place I am displaying the data – Junior Sep 28 '16 at 00:24
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/124359/discussion-between-stephen-muecke-and-mike-a). –  Sep 28 '16 at 00:26

1 Answers1

0

I don't see any kind of looping, but I think m.Access.UserToClients is a list, which you are passing to your template, however, your template is expecting a single item.

Update your template to take in a list:

@model IList<Proj.ViewModels.UserProfileAccessUserToClientsViewModel>

@foreach (var m in Model)
{
  <tr>
  ...
  </tr>
}
Andy T
  • 10,223
  • 5
  • 53
  • 95
  • The `EditorFor()` method accepts `IEnumerableT>` (OP's usage is correct) –  Sep 27 '16 at 23:50