Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7592977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:02:50+00:00 2026-05-30T21:02:50+00:00

I have a view that has two buttons(First and Second) and one hidden field.

  • 0

I have a view that has two buttons(“First” and “Second”) and one hidden field. When user clicks on “First” button, hidden field will be set as the value of “data-myAttribute” attribute in the first button. When user clicks on “Second” button, hidden field will be set as the value of “data-myAttribute” attribute in the second button. Once the hidden field is set, the form need to be submitted. This much is working fine with the following code. Further, I need to be able to see the value of the hidden field in the controller. How do we get the value in controller?

namespace MyHiddenFieldTest.Controllers
{
public class ElementController : Controller
{

    public class MyViewModel
    {
        public string ControlName { get; set; }
    }

    // GET: 
    public ActionResult MyElement()
    {
        MyViewModel myViewModel = new MyViewModel();
        return View(myViewModel);
    } 


    // POST:
    [HttpPost]
    public ActionResult MyElement(MyViewModel theViewModel)
    {
        string selectedControl = theViewModel.ControlName;
        return View(theViewModel);

    }


}
}

VIEW

@model MyHiddenFieldTest.Controllers.ElementController.MyViewModel

@{
ViewBag.Title = "MyElement";
}

<h2>MyElement</h2>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"     type="text/javascript"> </script>
<script type="text/javascript">


$(document).ready(function () 
{

    $('#mainDiv input[type="button"]').on('click', function () 
    {
        $('#from').val($(this).attr('data-myAttribute'));

        alert($('#from').val());
        $(this).closest('form').submit();
    });

});


</script>


@using (Html.BeginForm())
{

<div id="mainDiv">
    <input type="button" value="First" data-myAttribute="theFirst"  />
    <input type="button" value="Second" data-myAttribute="theSecond"  />
    <input type="hidden" id="from" value="1" />
    <input type="hidden" id="to" value="2" />
</div>

}

READING

  1. Passing JSON data from controller action to a razor view
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T21:02:52+00:00Added an answer on May 30, 2026 at 9:02 pm

    ASP.Net MVC automatically binds action parameters to form posts.
    Just add a string from parameter to the action method.

    However, you will also need to name="" the inputs in order for the browser to submit them.


    Actually, however, your entire script is unnecessary.

    Just write

    <input type="submit" name="from" value="First" />
    <input type="submit" name="from" value="Second" />
    

    The browser will submit the name / value pair of whichever button was clicked.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a view that has a navigationController with two buttons, START (essentially a
I have two views. The User view has some text and a button. I
I'm using MVC2 with VS2010 I have a view that has two partial views
I have an app that has a centre view with two views off to
I have two views controlled by a uinavigationcontroller. the first view has a UIWebView
I have two nib files, one that will open at startup and another that
I have 3 views in my application. My first view (FIRST_VIEW) has 2 buttons
I have a view that has a list of jobs in it, with data
I have a picker view that has a list of numbers to pick from.
I have a view controller class that has to implement several protocols. Too keep

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.