0
votes

I try to use the following expemple of Input Tag Helper in my code, but it isn´t working. https://docs.microsoft.com/de-de/aspnet/core/mvc/views/working-with-forms?view=aspnetcore-2.2 https://www.learnrazorpages.com/razor-pages/tag-helpers/input-tag-helper

I want to use a list of objects to display in a table and read there values from inputs. Display the data works, but not reading the Inputs.

cshtml

@page
@{
    var days2 = @Model.days;

}
<form class="form-horizontal" method="post">
    <button asp-page-handler="Go" type="submit" class="btn btn-default">Go</button>
    <table class="table">
        <thead>
            <tr>
                <th>@Html.DisplayNameFor(m => m.days[0].Name)</th>
                <th>@Html.DisplayNameFor(m => m.days[0].Beginn)</th>
                ........
            </tr>
        </thead>
        <tbody>
            @for (int i =0; i< days2.Count; i++)
            {
                <tr>
                    <td><input asp-for="@days2[i].Name" class="form-control" value=Tag.Name /></td>
                    <td><input asp-for="@days2[i].Beginn" class="form-control" value=Tag.Beginn /></td>
                    .....               
                </tr>
            }
        </tbody>


    </table>
</form>

cshtml.cs

[BindProperty]
public List<day> days { get; set; }

public void OnGet()
{
  days=getdaysvalue();
}

public void OnPostGo()
{
  for (int i = 0; i < Tage.Count; i++)
  {
    days[i].Name = days2[i].Name;  //days2 is in the actuell context not given
    days[i].Beginn = days2[i].Beginn;
  }

days2 is in the actuell context not given

2
Did you tried without value=Tag.Name ?sherox
What is the defination for Tage and what is the purpose for var days2 = @Model.days;?Edward
Tage is days. I change the german names of variables and there i´ve forgot it ;)Kai

2 Answers

0
votes

you have several properties with same name and an ID in the view, and in the end it just reads the value of one of the properties.

you can try this

           @for (int i =0; i< days2.Count; i++)
            {
                <tr>
                    <td><input id="Name_@i" type="text" name="Name" class="form-control" value="@Tag.Name" /></td>
                    <td><input id="Beginn_@i" type="text" name="Beginn" class="form-control" value="@Tag.Beginn" /></td>
                    .....               
                </tr>
            }

and in the RazorPage

public void OnPostGo(List<string> Name,List<string> Beginn)
{
  for (int i = 0; i < Tage.Count; i++)
  {
    days[i].Name = Name[i]; 
    days[i].Beginn = Beginn[i];
  }
}

0
votes

For Razor Page, it helps to bind the model with [BindProperty] public List<day> days { get; set; }, you should try to access the submit model by days instead of variable in view.

Try like:

  1. PageModel

    public class TestModel : PageModel
    {
        [BindProperty]
        public List<day> days { get; set; }
    
        public void OnGet()
        {
            days = new List<day> {
                new day{ Name = "D1", Beginn = "B1"},
                new day{ Name = "D2", Beginn = "B2"},
                new day{ Name = "D3", Beginn = "B3"},
            };
        }
    
        public void OnPostGo()
        {
            var result = days;
        }
    }
    
  2. View

    @page
    @model CoreRazor.Pages.Books.TestModel
    @{
        ViewData["Title"] = "Test";
    }
    
    <h1>Test</h1>
    
    <form class="form-horizontal" method="post">
        <button asp-page-handler="Go" type="submit" class="btn btn-default">Go</button>
        <table class="table">
            <thead>
                <tr>
                    <th>@Html.DisplayNameFor(m => m.days[0].Name)</th>
                    <th>@Html.DisplayNameFor(m => m.days[0].Beginn)</th>
                    ........
                </tr>
            </thead>
            <tbody>
                @for (int i = 0; i < Model.days.Count; i++)
                {
                    <tr>
                        <td><input asp-for="@Model.days[i].Name" class="form-control" /></td>
                        <td><input asp-for="@Model.days[i].Beginn" class="form-control" /></td>
                    </tr>
                }
            </tbody>
        </table>
    </form>