0

I want to know how can I override or set value for data-valmsg-for?

I have this partial view

  @model ANGS_SyncBook.Models.Transaction.JournalDetails

@{
    int i=ViewBag.i==null?0:ViewBag.i;
}
<tr scope="row">
    <td>
        @*<select id="selDocument" asp-for="Gla_Code" class="form-control form-control-sm" asp-items="@(new SelectList(ViewBag.ChartOfAccounts,"Gla_Code","Gla_Name"))"></select>*@
        @*<select id="selChartofAcc" asp-for="Gla_Code" class="form-control form-control-sm"></select>*@
        @Html.DropDownList("[" + i + "].Gla_Code", new SelectList(ViewBag.ChartOfAccounts, "Gla_Code", "Gla_Name"), new { @class = "form-control form-control-sm", @id= "[" + i + "].Gla_Code" })
        @Html.ValidationMessageFor(model => model.Gla_Code, null, new { @Name = "[" + i + "].Gla_Code" } )
    </td>

Sample output of partial view

enter image description here

I want to validate every field that is required but it is not working because the Id is dynamically created

Is there something like?

@Html.ValidationMessageFor(model => model.Gla_Code, null, new { @Name = "[" + i + "].Gla_Code", @data-valmsg-for="[" + i + "].Gla_Code" } )
Mike Brind
  • 28,238
  • 6
  • 56
  • 88
  • Why in the world would you ever want to do that (and screw up model binding) –  Aug 29 '18 at 06:52
  • 1
    And I can only assume you think you need that for dynamically adding a new row? Suggest you look at the code in [this answer](http://stackoverflow.com/questions/28019793/submit-same-partial-view-called-multiple-times-data-to-controller/28081308#28081308) and [this one](http://stackoverflow.com/questions/40539321/partial-view-passing-a-collection-using-the-html-begincollectionitem-helper/40541892#40541892) for generating your view –  Aug 29 '18 at 06:54
  • @StephenMuecke, yes .user can add number of rows, i just want to do per row validation before user saves data. everything is ok, just the validation is the problem – IAmANoobProgrammer Aug 29 '18 at 07:02
  • 1
    Look at the links I gave you to do this correctly - there is no point showing you how to do another hack to fix up code which is fundamentally wrong in the first place –  Aug 29 '18 at 07:04
  • @StephenMuecke will check sir, thank you – IAmANoobProgrammer Aug 29 '18 at 07:08

1 Answers1

0

Solve the issue using

    @using(Html.BeginCollectionItem("JDetails"))
{
<tr scope="row">
    <td>
        @Html.DropDownListFor(model => model.Gla_Code, new SelectList(ViewBag.ChartOfAccounts, "Gla_Code", "Gla_Name"))
        @Html.ValidationMessageFor(model => model.Gla_Code, null)
    </td>
</tr>

etc..
}