0

How to fetch HTML Input Id in code behind ASP.Net MVC ? Either on Controller or on Model ?

view / .cshtml

<button type="submit" id="previewButton" >Preview</button>

JQuery : - This will not be in use.

$("button").click(function ()
    {
        var btn = this.id;
          alert(btn);
   }

In Controller :

[HttpPost]
        public ActionResult New(MyViewModel viewModel, string clickedButton)
        {

            var vm = clickedButton;
            return View(vm);
        }
  • MyViewModel

    public string clickedButton { get; set; }

tereško
  • 58,060
  • 25
  • 98
  • 150
goofyui
  • 3,362
  • 20
  • 72
  • 128
  • 1
    A form posts back the name/value pairs of its successful form controls (not its `id` attribute). Make the button ``, and if you click that button, the value of your parameter will be "xxxx" –  Aug 10 '18 at 04:36
  • But if you have a property for it in the model, then you do not need the `string clickedButton` parameter (the value of the property will be bound with the value of the button). –  Aug 10 '18 at 04:37
  • @StephenMuecke , In this syntax .. – goofyui Aug 10 '18 at 04:44
  • Exactly as per my first comment - you need a `name` attribute which must match the name of the parameter (or model property), and a `value` attribute (and the parameter or property will be bound with that value) –  Aug 10 '18 at 04:46
  • i am not able to retrieve the name (or) id – goofyui Aug 10 '18 at 04:57
  • Then I can only assume you did not click on that button –  Aug 10 '18 at 04:58

0 Answers0