Home > other >  Getting Inner Value of "name" / "value" of button in Razor Pages Asp.net 6
Getting Inner Value of "name" / "value" of button in Razor Pages Asp.net 6

Time:05-25

working on something and stuck at this:

My Razor Page looks like this:

 <tbody>
         @foreach(var item in Model.Terminy)
        {
            <tr>
            <form method="post"> <td> <center><button asp-page-handler="edit"  value="@item.Dzień_Tygodnia">@item.Dzień_Tygodnia</button><br /></center></td></form>
           <form method="post"><td> <center><button asp-page-handler="edit"  name="@item.Dzień_Tygodnia" value="@item.Godzina">@item.Godzina</button></center></td></form>
           <form method="post"> <td> <center><button asp-page-handler="edit"  name="@item.Dzień_Tygodnia" value="@item.Godzina1">@item.Godzina1</button></center></td></form>
           <form method="post"> <td> <center><button asp-page-handler="edit"  name="@item.Dzień_Tygodnia" value="@item.Godzina2">@item.Godzina2</button></center></td></form>
           <form method="post"> <td> <center><button asp-page-handler="edit"  name="@item.Dzień_Tygodnia" value="@item.Godzina3">@item.Godzina3</button></center></td></form>
           <form method="post"> <td> <center><button asp-page-handler="edit"  name="@item.Dzień_Tygodnia" value="@item.Godzina4">@item.Godzina4</button></center></td></form>
            <form method="post"> <td> <center><button asp-page-handler="edit"  name="@item.Dzień_Tygodnia" value="@item.Godzina5">@item.Godzina5</button></center></td></form>
             <form method="post"><td> <center><button asp-page-handler="edit"  name="@item.Dzień_Tygodnia" value="@item.Godzina6">@item.Godzina6</button></center></td></form>
            <form method="post"> <td> <center><button asp-page-handler="edit"  name="@item.Dzień_Tygodnia" value="@item.Godzina7">@item.Godzina7</button></center></td></form>
             <form method="post"><td> <center><button asp-page-handler="edit"  name="@item.Dzień_Tygodnia" value="@item.Godzina8">@item.Godzina8</button></center></td></form>
             <form method="post"><td> <center><button asp-page-handler="edit"  name="@item.Dzień_Tygodnia" value="@item.Godzina9">@item.Godzina9</button></center></td></form>
            </tr>
        }
        
    </tbody>

This is single element:

<form method="post"><td> <center><button asp-page-handler="edit"  name="@item.Dzień_Tygodnia" value="@item.Godzina">@item.Godzina</button></center></td></form>

Now the thing is: In Model class on my OnPostAsync method i need to get inner value of "value" and "name" after click on button.

public async Task<IActionResult> OnPostAsync()
        
{

}

Stuck at this since 5P.M and now is 7P.M and found nothing. Hope you guys can help me find this out. Will be greatful.

CodePudding user response:

I think you may misunderstand form submit. It can only post the value of input/select/textarea element. And it cannot post the name attribute's value to the backend. name attribute is used for model binding, bind the property with the same name of the name attribute value.

One way is to add hidden two inputs to every form:

@page
@model IndexModel

<table>
    <tbody>
         @foreach(var item in Model.Terminy)
        {
            <tr>
            <form method="post"> 
                <td> 
                    <center>
                        <input name="Dzień_Tygodnia" value="@item.Dzień_Tygodnia" hidden/>
                        <button asp-page-handler="edit"  value="@item.Dzień_Tygodnia">@item.Dzień_Tygodnia</button><br />
                    </center>
                </td>
           </form>
           <form method="post">
               <td> 
                   <center>
                       <input name="Dzień_Tygodnia" value="@item.Dzień_Tygodnia" hidden/>
                       <input name="Godzina" value="@item.Godzina" hidden/>
                       <button asp-page-handler="edit"  name="@item.Dzień_Tygodnia" value="@item.Godzina">@item.Godzina</button>
                   </center>
               </td>
            </form>
           <form method="post"> 
               <td> 
                   <center>
                       <input name="Dzień_Tygodnia" value="@item.Dzień_Tygodnia" hidden/>
                       <input name="Godzina1" value="@item.Godzina1" hidden/>
                       <button asp-page-handler="edit"  name="@item.Dzień_Tygodnia" value="@item.Godzina1">@item.Godzina1</button>
           </center></td></form>
           <form method="post"> <td> <center>
               <input name="Dzień_Tygodnia" value="@item.Dzień_Tygodnia" hidden/>
               <input name="Godzina2" value="@item.Godzina2" hidden/>
               <button asp-page-handler="edit"  name="@item.Dzień_Tygodnia" value="@item.Godzina2">@item.Godzina2</button></center></td></form>
           <form method="post"> <td> <center>
               <input name="Dzień_Tygodnia" value="@item.Dzień_Tygodnia" hidden/>
                <input name="Godzina3" value="@item.Godzina3" hidden/>
               <button asp-page-handler="edit"  name="@item.Dzień_Tygodnia" value="@item.Godzina3">@item.Godzina3</button></center></td></form>
            </tr>
       //more forms..........
        }
        
    </tbody>
</table>

Backend:

public IActionResult OnPostEdit(Terminy model)
{
    //do your stuff.........
    
}

Model:

public class Terminy
{
    public string Dzień_Tygodnia { get; set; }
    public string Godzina { get; set; }
    public string Godzina1 { get; set; }
    public string Godzina2 { get; set; }
    public string Godzina3 { get; set; }
    //other properties...........
}

Another way is that you can use asp-route-{value} tag helper:

@page
@model IndexModel

<table>
    <tbody>
        @foreach(var item in Model.Terminy)
        {
            <tr>
            <form method="post"> <td> <center><button  asp-route-name="" asp-route-value="@item.Dzień_Tygodnia">@item.Dzień_Tygodnia</button><br /></center></td></form>
           <form method="post"><td> <center><button  asp-route-name="@item.Dzień_Tygodnia" asp-route-value="@item.Godzina">@item.Godzina</button></center></td></form>
           <form method="post"> <td> <center><button  asp-route-name="@item.Dzień_Tygodnia" asp-route-value="@item.Godzina1">@item.Godzina1</button></center></td></form>
           <form method="post"> <td> <center><button  asp-route-name="@item.Dzień_Tygodnia" asp-route-value="@item.Godzina2">@item.Godzina2</button></center></td></form>
           <form method="post"> <td> <center><button  asp-route-name="@item.Dzień_Tygodnia" asp-route-value="@item.Godzina3">@item.Godzina3</button></center></td></form>
           @*//other forms...........             *@
            </tr>
        }
        
    </tbody>
</table>

Backend:

public async Task<IActionResult> OnPostAsync(string name,string value)
{
    //do your stuff.........
}
  • Related