Change button text onclick with condition

爷,独闯天下 提交于 2019-12-11 20:18:59

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!