I to needed to solve the same problem using ASP.NET MVC but couldn't use the solution suggested above because my project uses localisation. The problem with the above solution is that it uses the value attribute of the <input type="submit"> button which
browsers use as the display text of the button. Effectively the check is being performed on the text label of the button which is an issue for solutions using localisation which sets the label of the button according to the detected locale of the user. Also,
if you change the text of the button in the HTML/ASPX you have to edit and re-compile the back-end code to switch on the new values else the logic breaks.
To solve thse issues my form uses HTML <button type="submit"> buttons:
The Controller ActionResult can then switch on the value of the "value" attribute yet remain disconnected from the text of the button itself.
I understand that the HTML <button> element isn't 100% implemented as well as the standard <input type="submit"> element in some browsers but I really can't be bothered if it doesn't work with 0.0001% of internet users that still insist on using Netscape
Navigator 4.
I think the solution that I suggested can still work even with localization. Just add a small function to translate.
Example:
(without JavaScript)
on your .aspx page, add a second button:
<input type="submit" value="localizedValueA"
name="submitButton"/>
<input type="submit"
value="localizedValueB" name="submitButton"/>
in your Controller:
// POST: /Home/Edit/5
[AcceptVerbs(HttpVerbs.Post)]
public
ActionResult Edit(Movie movieToEdit,
string
submitButton)
{
string translatedButtonValue; translatedButtonValue = translateButtonValue(submitButton);
if (translatedButtonValue == "First)
et cetera
Regards,
Gerry (Lowry)
B-) Please help me by completing my school survey about computer programmers on my website. Thank you!!! Gerry Lowry +1 705-429-7550 wasaga beach, ontario, canada
I have tried the solution with submitButton as Button-Name and Action-Parameter, but the value is always null.
Is there some expected precondition, that this works?
---------- EDIT -------------
I just realized, that it works with Html.BeginForm, but not with
Ajax.BeginForm ... any idea or hint to get it work on Ajax-Forms?
---------- EDIT -------------
I'm not sure. view source regarding your rendered page and study it. compare your Ajax to your non Ajax.
Problem with Ajax is you are only rendering part of the page.
If an Ajax action creates a need for the entire page to be rendered, I'm not sure how you would handle that.
I'm guessing with Ajax you likely have a callback function ~~ maybe you would have to tell the callback function to render the entire page.
I'm just making wild guesses here. You could also examine the Google language tools pages ~~ they have various sections for switching languages, et cetera. I have not examined them; nevertheless, there's a good chance Google is using Ajax; problem is with
Google, you will not be able to see what they are doing on the server side.
g.
B-) Please help me by completing my school survey about computer programmers on my website. Thank you!!! Gerry Lowry +1 705-429-7550 wasaga beach, ontario, canada
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Tester(FormCollection collection)
{
//string saveButton, string cancelButton
if (collection["register"] != null) {
ViewData["Message"] = "you wanna save"; // redirect to appropriate action
}
else if (collection["cancel"] != null)
{
ViewData["Message"] = "you wanna cancel"; // redirect to appropriate action
}
else
{
ViewData["Message"] = "what de hell are you trying to do?"; // redirect to appropriate action
}
return View();
}
“Programmers are in a race with the Universe to create bigger and better idiot-proof programs, while the Universe is trying to create bigger and better idiots. So far the Universe is winning.”
(Rich Cook)
vinothkumars...
Member
36 Points
36 Posts
Re: submit Button In Mvc
Apr 28, 2009 10:54 AM|LINK
peterduerden
Member
2 Points
1 Post
Re: submit Button In Mvc
May 08, 2009 10:48 AM|LINK
I to needed to solve the same problem using ASP.NET MVC but couldn't use the solution suggested above because my project uses localisation. The problem with the above solution is that it uses the value attribute of the <input type="submit"> button which browsers use as the display text of the button. Effectively the check is being performed on the text label of the button which is an issue for solutions using localisation which sets the label of the button according to the detected locale of the user. Also, if you change the text of the button in the HTML/ASPX you have to edit and re-compile the back-end code to switch on the new values else the logic breaks.
To solve thse issues my form uses HTML <button type="submit"> buttons:
<form action="/Track/GetFolderId" method="post"> <fieldset> <legend>FolderId</legend> <p> <label for="folderid">FolderId:</label> <input id="folderid" name="folderid" type="text" value="" /> </p> <p> Separate multiple FolderIds with commas. </p> <p> <button type="submit" name="submitbutton" value="getheadlines">Get Headlines</button> <button type="submit" name="submitbutton" value="getnewheadlines">Get New Headlines</button> </p> </fieldset> </form>The Controller ActionResult can then switch on the value of the "value" attribute yet remain disconnected from the text of the button itself.
I understand that the HTML <button> element isn't 100% implemented as well as the standard <input type="submit"> element in some browsers but I really can't be bothered if it doesn't work with 0.0001% of internet users that still insist on using Netscape Navigator 4.
gerrylowry
All-Star
20515 Points
5713 Posts
Re: submit Button In Mvc
May 08, 2009 12:55 PM|LINK
Hello Peter,
I think the solution that I suggested can still work even with localization. Just add a small function to translate.
Example:
(without JavaScript)
on your .aspx page, add a second button:
<input type="submit" value="localizedValueA" name="submitButton"/>
<input type="submit" value="localizedValueB" name="submitButton"/>
in your Controller:
// POST: /Home/Edit/5
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(Movie movieToEdit, string submitButton)
{
string translatedButtonValue;
translatedButtonValue = translateButtonValue(submitButton);
if (translatedButtonValue == "First) et cetera
Regards,
Gerry (Lowry)
darkprojekt
Member
8 Points
6 Posts
Re: submit Button In Mvc
May 10, 2009 08:18 PM|LINK
Hi
I have tried the solution with submitButton as Button-Name and Action-Parameter, but the value is always null.
Is there some expected precondition, that this works?
---------- EDIT -------------
I just realized, that it works with Html.BeginForm, but not with Ajax.BeginForm ... any idea or hint to get it work on Ajax-Forms?
---------- EDIT -------------
Thanks for any hint
Michael
gerrylowry
All-Star
20515 Points
5713 Posts
Re: submit Button In Mvc
May 10, 2009 09:36 PM|LINK
I'm not sure. view source regarding your rendered page and study it. compare your Ajax to your non Ajax.
Problem with Ajax is you are only rendering part of the page.
If an Ajax action creates a need for the entire page to be rendered, I'm not sure how you would handle that.
I'm guessing with Ajax you likely have a callback function ~~ maybe you would have to tell the callback function to render the entire page.
I'm just making wild guesses here. You could also examine the Google language tools pages ~~ they have various sections for switching languages, et cetera. I have not examined them; nevertheless, there's a good chance Google is using Ajax; problem is with Google, you will not be able to see what they are doing on the server side.
g.
darkprojekt
Member
8 Points
6 Posts
Re: submit Button In Mvc
May 22, 2009 08:45 PM|LINK
lenocin
Member
206 Points
148 Posts
Re: submit Button In Mvc
May 23, 2009 08:34 AM|LINK
I just quickly scanned the postings, but sounds the "problem" is mulitple submit buttons on a form?
Here's what I do, and no need using javascripts and stuff (people still remember javascript / ajax / etc is for enhancing existing functional code? )
View Form:
<% Html.BeginForm("Tester", "Home", FormMethod.Post); %> <input type="submit" name="register" value="Register" /> <input type="submit" name="cancel" value="Cancel" /> <% Html.EndForm(); %>Controller Action:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Tester(FormCollection collection)
{
//string saveButton, string cancelButton
if (collection["register"] != null) {
ViewData["Message"] = "you wanna save"; // redirect to appropriate action
}
else if (collection["cancel"] != null)
{
ViewData["Message"] = "you wanna cancel"; // redirect to appropriate action
}
else
{
ViewData["Message"] = "what de hell are you trying to do?"; // redirect to appropriate action
}
return View();
}
(Rich Cook)