0

I have a list of items in my view and each has the below submit input type. Each input type has a dynamic value that is then passed through to the parameter 'int button' so the controller is aware of which button has been selected. View

<input type="submit" name="button" value="@Model.Sites[i].Id" />

Controller

[HttpPost]
public ActionResult ViewInvoice(List<string> invoice, List<int> site, int button = 0)
{
}

The dynamic value is posted correctly to the controller however the buttons are named in the view according to this dynamic value "@Model.Sites[i].Id". How can i set all the button names to "Select" while keeping the dynamic value for my controller?

Thanks in advance.

LiamJenks
  • 1
  • 1

2 Answers2

2

try using this:

<button type="submit" name="button" value="@Model.Sites[i].Id">Select</button>
Matt Tabor
  • 1,053
  • 8
  • 10
  • Thanks for this. I have given it a go and when running locally through visual studio is works perfectly however when released to the server it doesn't seem to be passing through the dynamic value "button" anymore. Any suggestions? – LiamJenks Feb 19 '14 at 16:13
0

the timing will be a little tricky but you can try this

<input type="submit" value="Select" class="btnSubmit1" />

then in your script set a hidden field based on the button click

$(document).ready(function(){
    $('.btnSubmit1').on('click', function(){
        $('.hdnButton').val('btnSubmit1');
    });
});

If the form submits before the field is set then you may need to change the button type to button, set the field and submit the form via ajax Submit a form using jQuery

Community
  • 1
  • 1
Matt Bodily
  • 6,403
  • 4
  • 29
  • 48