Home > front end >  How I can assign dynamic asp-for in razor view
How I can assign dynamic asp-for in razor view

Time:09-15

        @for (var i = 1; i < 5; i  )
        {
            var Tier = $"Tier{i}Score";
            <div >
                <div >
                    <div >
                        <label>@i</label>
                    </div>
                    <div >
                        <div >
                            <label>@localizer[$"Label_Tier{i}Description"]</label>
                            <i  data-toggle="tooltip" data-placement="right" title="@localizer[$"Tooltip_Tier{i}Score"]"></i>
                        </div>
                    </div>
                    <div >

                        <input asp-for="@Tier" type="text"  maxlength="4" autocomplete="off" />
                    </div>
                </div>
            </div>
        }

When my div is generated, my id just display as id=Tier, I would like to get result id=Tier1Score, id=Tier2Score, id=Tier3Score and id=Tier4Score. How I can achieve this. Thank you.

CodePudding user response:

You can't also see this answer: Dynamically assigning asp-for variables

But since it's just a TagHelper (it doesn't do anything besides writing correct HTML) you could also write it like this:

 <input name="@Tier" id="@Tier" type="text"  maxlength="4" autocomplete="off" />

The name is important for the form-post, the id is probably used for javascript thingies you might want to do.

  • Related