2

How to get the value of an input button in an ASP.NET Core MVC controller?

This is my html code in the view

<html>
    <body>
        <form asp-controller="Home" asp-action="GetstartTime" method="post" id="TableF">
            <input id="1" name="f1" form="TableF" class="btn btn-primary" type="submit" value="12:00 PM"/>
            <input id="2" name="f2" form="TableF" class="btn btn-primary" type="submit" value="2:00 PM"/>
            <input id="3" name="f3" form="TableF" class="btn btn-primary" type="submit" value="3:00 PM"/>
        </form>
    </body>
</html>
5
  • this.Request.Form["f0"] should do fine. Assuming you have a form there. Commented Nov 30, 2019 at 20:35
  • You could use BeginForm like this stackoverflow.com/questions/18873098/… Commented Nov 30, 2019 at 20:38
  • @WiktorZychla we have multi buttons so i don't know which id will be come to the controller Commented Nov 30, 2019 at 20:40
  • @MinaEssam: This still is not clear enough. Try to expand your example to show what your actual issue really is. At the moment, your html doesn't even have any form, it doesn't make much sense to reason about what happens at the server. Commented Nov 30, 2019 at 20:43
  • @WiktorZychla Please look to my update Commented Nov 30, 2019 at 21:08

1 Answer 1

5

in the razor :

<form asp-controller="Controller" asp-action="PostData">
<input  id="f0"  name="f0" form="TableF" class="btn btn-primary" type="submit" value="12:00 PM"/>
</form>

in the controller :

 [HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> PostData(string f0)
{
    var buttonvalue =f0;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.