0

Say I have a model that maps a person and I want to build a view for this model having some text inputs and a series of drop-downs to select state, province, city.

I'm adding @onchange = "this.form.submit()" in each @Html.DropDownListFor to post back to the server and drill-down the selection on those drop-downs.

How can I understand in my controller the source of said post? How do I distinguish the post coming from a drop-down change to the one coming from a submit button?

ccalboni
  • 12,120
  • 5
  • 30
  • 38
  • 1
    "> I'm adding `@onchange = "this.form.submit()"` in each `@Html.DropDownListFor` to post back to the server and drill-down the selection on those drop-downs." - **please don't do this** for many reasons (namely, it breaks the user's back button) and can be a jarring user-experience. Instead use a normal AJAX request. – Dai Feb 17 '20 at 10:31

1 Answers1

0

While not being able to understand the exact source of the postback, based on this question and answers I'm at least able to check if the source was one of the submit buttons by simply checking:

if(Request.Form["submitButtonNameAttribute"] != null)

When multiple buttons are available, only the clicked one is != null at postback.

Still, this is not a complete solution related to the original answer, but while a button may indicate an action to be run, other changes in the view may just invoke some sync work on the model.

ccalboni
  • 12,120
  • 5
  • 30
  • 38