简体   繁体   中英

How to pass value from _layout to controller asp.net

I have this button in my layout that changes value with a click event:

<input type="button" id="switchbutton" value="Weekend" style="color:blue" onclick="toggle(this);">
                                <script type="text/javascript"></script>
                                <script>
                                    function toggle(button) {
                                        switch (button.value) {
                                        case "Weekend":
                                            button.value = "Week";
                                            break;
                                        case "Week":
                                            button.value = "Weekend";
                                            break;
                                        }
                                        return value;
                                    }
                                </script>

And I need to send the value of the button to my controller to use it on a condition:

if (Button.value =="Weekend")
{
Do something
}
else
{
Do antoher thing
}

Could you please help me to send the value from the button on my layout to my controller?

You should be able to get the value similar to the view, on your form post.

public ActionResult Edit([Bind(Include = "switchbutton")] YourModel yourModel){
string myValue = yourModel.switchbutton;}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM