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; }

6
  • 1
    A form posts back the name/value pairs of its successful form controls (not its id attribute). Make the button <button type="submit" name="clickedButton" value="xxxx">Preview</button>, and if you click that button, the value of your parameter will be "xxxx" Commented Aug 10, 2018 at 4: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). Commented Aug 10, 2018 at 4:37
  • @StephenMuecke , In this syntax .. <button type="submit" id="previewButton" > - You want me to replace Id with name ? Is that right? Doing so will suffice the problem ? Commented Aug 10, 2018 at 4: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) Commented Aug 10, 2018 at 4:46
  • i am not able to retrieve the name (or) id Commented Aug 10, 2018 at 4:57

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.