Mvc - 将viewmodel属性绑定到切换按钮 [英] Mvc - binding viewmodel property to toggle button

查看:153
本文介绍了Mvc - 将viewmodel属性绑定到切换按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将MVC ViewModel中的属性绑定到切换按钮?



我尝试过:



我尝试使用基本的JQuery手动绑定...例如。 $('#btnToggle')。click()..

然后根据切换将HiddenField值设置为true / false。



我确信在MVC中有一个更好的控制绑定解决方案。

解决方案

('#btnToggle')。click()..

然后根据切换将HiddenField值设置为true / false。



我确信MVC中的控件绑定有更好的解决方案。


如果你想要保留表单帖子中没有包含的内容(例如选项是打开还是关闭),那么唯一的方法就是将数据存储在隐藏字段中现在正在做。


为了将服务器视图模型中的属性绑定到客户端的JavaScript属性,可以使用我的 WebBinding



在客户端,可以影响HTML元素根据JavaScript属性。例如,您可以使用JavaScript MVC库(例如: AngularJS ng-class Knockout css binding 等... )使用像这样的简单的HTML切换按钮来切换课程。


Is it possible to bind a property from the MVC ViewModel to a toggle button?

What I have tried:

I have tried using basic JQuery to manually bind using...eg. $('#btnToggle').click()..
then set the HiddenField value to true/false based on the toggle.

I'm sure there's a better solution for control binding in MVC.

解决方案

('#btnToggle').click()..
then set the HiddenField value to true/false based on the toggle.

I'm sure there's a better solution for control binding in MVC.


If you want to persists something that is not included in a form post (such as if an option is on or off) then the only way to do so is to store the data in a hidden field as you are doing now.


For binding a property in the server's view-model to a JavaScript property at the client side, you can use my WebBinding.


In the client side, you can affect the HTML element according to the JavaScript property. For instance, you can use a JavaScript MVC library (like: AngularJS ng-class or, Knockout css binding, etc...) to toggle class with someting like this Simple HTML Toggle Button.


这篇关于Mvc - 将viewmodel属性绑定到切换按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆