I have a for loop to generate fields on a form.
How can I set the value of asp-for based on the current index?
<label asp-for=@("Value" + i) class="control-label"></label>
does not work.
I have a for loop to generate fields on a form.
How can I set the value of asp-for based on the current index?
<label asp-for=@("Value" + i) class="control-label"></label>
does not work.
You can bind the model with an index like so using ether foreach
or for
:
@page
@model IndexModel
@{
ViewData["Title"] = "Home page";
int i = 0;
}
<form method="post">
@foreach (var item in Model.Items)
{
<input asp-for="Items[i]" />
i++;
}
@for (int j = 0; j < Model.Items.Count; j++)
{
<input asp-for="Items[j]" />
}
<button type="submit">Submit</button>
</form>
And the code behind:
public class IndexModel : PageModel
{
[BindProperty]
public List<string> Items { get; set; }
public void OnGet()
{
Items = new List<string> { "one", "two", "three" };
}
public void OnPost(List<string> items)
{
}
}
Here is a bit of how it works:
In this controller you have 2 action, one returns a list of strings which will be out model. And the second action will accept a parameter as a list of strings.
[Route("[controller]")]
public class TestController : Controller
{
[HttpGet("[action]")]
public IActionResult Test()
{
return View(new List<string> { "one", "two", "three" });
}
[HttpPost("[action]")]
public IActionResult Test(List<string> Words)
{
return Ok();
}
}
Now in our Test view Test.cshtml
file, we want to display this list of string with their index and when we change these values we want to be able to post them.
@model List<string>
@{
int i = 0;
}
<form method="post">
@foreach (var item in Model)
{
<label>@item - @i</label>
<input name="Words[@i]" value="@item" />
i++;
}
<button type="submit">Submit</button>
</form>
Here is the result:
And after submitting the form, the values in the input fields will appear in the list of strings, because of the html name
attribute.
You have to understand that the tag helper asp-for
is used by Razor to generate all sorts of html attributes on the field such as name/id/data-* etc... the main thing you need is the name
attribute to bind the value.
@for(int i = 1; i <= 31; i++) { <label asp-for="Value"+i /> <input type="number" asp-for"Value"+i /> }
This way I don't have to write 62 lines for my 31 properties –
Madi asp-for
? –
Gunning © 2022 - 2024 — McMap. All rights reserved.
<label for="someid">
?! When you loop through an array you already have the reference to the item. i. .foreach(var item in Model) { ...}
then you have a reference toitem
, so you can use it inasp-for
likeasp-for="item.Value"
orasp-for="item.Name"
depending on how your properties on the model class are named – Scalawag@("Value" + i)
? what did you expect instead ? Is there an error message ? – Misdirectionasp-for
must be aModelExpression
. It can't take a generic string value representing a property. FWIW, though, having properties likeValue1
,Value2
,Value3
, etc. is pretty much always wrong. Use a collection type, instead: i.e.Values
. Then, what you're attempting to do here is trivial, since you're just indexing that collection, which can be expressed as aModelExpression
. – Toothfor(int i = 1; i <= 31; i++) { /* tag helper for each property */ }
Wndrr >>> I get an error InvalidOperationException: Templates can be used only with field access, property access, single... – Madi