问题
My aim is to have a button, with default value Block and when the user click on it, it will change the text to Unblock and also after page refresh the text of the button must remain the same, if the text is changed to unblock then after refresh it must remain unblock.
What i have tried uptil now is this:
View code for Button:
input type="submit" value="@ViewBag.SubmitValue" id="Block" style="color: white;
background-color: darkred; border-bottom-left-radius: 2px; border-bottom-right-radius: 2px;
border-top-left-radius: 2px; border-top-right-radius: 2px; padding: 4px 4px;
border: none; padding-bottom:2px "
Controller ActionResult
public ActionResult Block(int Id, Block block, string userAction)
{
if(userAction == "Block")
{
ViewBag.SubmitValue = "Block";
}
if (userAction == "Unblock")
{
ViewBag.SubmitValue = "Unblock";
}
.....
}
I am haivng issue in this passing string userAction in ActionResult, the value of userAction is passing as Null in the Method, so the text of button is not changing and it is showing "Submit" on button
Please help
回答1:
use a hidden input element named "userAction" and set its value to whatever you need.
<input type="hidden" name="userAction" value="@ViewBag.SubmitValue">
and for default action set the initialized value of userAction like this:
public ActionResult Block(string userAction = "Block", int Id, Block block){
//...
}
whenever you passed nothing named userAction it will be set to "Block" not null
来源:https://stackoverflow.com/questions/27758037/change-button-text-onclick-with-condition