lundi 2 novembre 2015

Binding view table data to MVC model

I am working on an existing project, someone wrote a view and is attempting to bind table data to a model list, It doesn't work and my skills are not up the task of figuring out why

heres the javascript line I assume is supposed to set the binding

$("#benefitsTable").bindmvclist();

and here is the table inside the view

Benefit Type Amount Expiry Date

                @* The insert template is only needed when the form is unlocked and therefore editable. *@
                @if (Model.IsUnlocked)
                {
                    <div data-type="itemtemplate" class="benefits-row">
                        @if (Model.CanEdit)
                        {
                            <input id="Benefits_{0}__AllowanceId" name="Benefits_[{0}].AllowanceID" type="hidden" value="0"/>
                            <select id="Benefits_{0}__AllowanceTypeId" name="Benefits[{0}].AllowanceTypeID" style="width: 220px">
                                @foreach (var allowance in Model.AllowanceTypesBenefits)
                                {
                                    <option value="@allowance.Value">@allowance.Text</option>
                                }
                            </select>
                            <input id="Benefits_{0}__AllowanceAmount" name="Benefits[{0}].Amount" type="text" value="" style="width: 68px;"/>
                            <input id="Benefits_{0}__ExpiryDate" name="Benefits[{0}].ExpiryDate" type="text" value="" class="datepickerrecurring datepicker" style="width: 80px"/>
                            <a class="removeBenefit" href="#"><i class="icon-close-delete"></i></a>
                        }
                        else if (Model.CanView)
                        {
                            <p>@Model.Benefits[0].AllowanceTypeName</p>
                        }
                    </div>
                }
                @if (Model.Benefits != null)
                {
                            @* Do not turn into a foreach. *@
                    for (var i = 0; i < Model.Benefits.Count(); i++)
                    {
                        <div class="benefits-row">
                            @Html.HiddenFor(a => Model.Benefits[i].AllowanceID) @* This somehow magically turns into id="Benefits_0__AllowanceTypeID" name="Benefits[0].AllowanceTypeID", which is why a foreach won't work. *@

                            @if (Model.IsUnlocked)
                            {
                                if (Model.CanEdit)
                                {
                                    @Html.DropDownListFor(a => Model.Benefits[i].AllowanceTypeID, new SelectList(Model.AllowanceTypesBenefits, "Value", "Text", Model.Benefits[i].AllowanceTypeID), new {@style = "width:220px;"}, Model.CanView)
                                    @Html.TextBoxFor(a => Model.Benefits[i].Amount, Model.CanView, new {maxlength = "100", size = "8", Value = String.Format("{0:0.00}", Model.Benefits[i].Amount)})
                                    @Html.ValidationMessageFor(m => Model.Benefits[i].Amount)

                                    @Html.TextBoxFor(a => Model.Benefits[i].ExpiryDate, new {@class = "datepicker", @style = "width: 80px;", Value = String.Format("{0:d/MM/yyyy}", Model.Benefits[i].ExpiryDate), @readonly = "readonly"})
                                    @Html.ValidationMessageFor(m => Model.Benefits[i].ExpiryDate)
                                    <a class="removeBenefit" href="#"><i class="icon-close-delete"></i></a>
                                }
                                else if (Model.CanView)
                                {
                                    <span class="benefitLabel">@Model.Benefits[i].AllowanceTypeName</span>
                                    @Html.TextBoxFor(a => Model.Benefits[i].Amount, Model.CanView, new {maxlength = "100", size = "8", Value = String.Format("{0:0.00}", Model.Benefits[i].Amount), @readonly = "readonly", disabled = "disabled"})
                                    @Html.TextBoxFor(a => Model.Benefits[i].ExpiryDate, new {@style = "width: 80px;", Value = String.Format("{0:d/MM/yyyy}", Model.Benefits[i].ExpiryDate), @readonly = "readonly", disabled = "disabled"})
                                }
                            }
                            else
                            {
                                <span class="benefitLabel">@Model.Benefits[i].AllowanceTypeName</span>
                                <input type="text" size="8" value="###" disabled="disabled"/>
                                @Html.TextBoxFor(a => Model.Benefits[i].ExpiryDate, new {@class = "datepicker", @style = "width: 80px;", Value = String.Format("{0:d/MM/yyyy}", Model.Benefits[i].ExpiryDate), @readonly = "readonly", disabled = "disabled"})

                            }
                        </div>
                    }
                }
            </div>
        </fieldset>

Any help/guidance would be greatly appreciated :)

NOTE: evertime the page is loaded the console logs the following error: "Uncaught TypeError: $(...).bindmvclist is not a function"

Aucun commentaire:

Enregistrer un commentaire