how to call a controller method from razor view text input field

Padma Anantha Krishnan 0 Reputation points
2023-03-16T10:02:17.9766667+00:00

how to call a controller method from razor view text input field

Developer technologies ASP.NET Other
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. QiYou-MSFT 4,326 Reputation points Microsoft External Staff
    2023-03-17T06:57:20.8+00:00

    Hi @Padma Anantha Krishnan

    Pass the value to the backend through input, and finally use the Razor method to pass the value of the backend to the frontend.

    Code:

    <form action="/Home/About" method="post">
    <div>
        <input type="text" name="text"/>
        <input type="submit" value="button"/>
    </div>
    <div>
        <a href=@Url.Action(@ViewBag.Message)>Action</a>
    </div>
    </form>
    
    public class HomeController : Controller
        {
            public ActionResult Index()
            {
                return View();
            }
            public ActionResult About()
            {
                string text=Request.Form["text"];
                ViewBag.Message = text;
                return View();
            }
            public ActionResult Contact()
            {
                ViewBag.Message = "Your contact page.";
                return View();
            }
          
            {
               
                return "static name";
            }
            public string GetName1()
            {
                return "name";
            }
           
        }
    

    Result:

    MVC

    Best regards,
    Qi You


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.