【问题标题】:MVC and show/hinde panels?MVC 和显示/隐藏面板?
【发布时间】:2010-06-24 09:15:50
【问题描述】:

我在我的 MVC 中使用强类型视图,现在我可以根据自己的喜好显示注册表单的所有编辑器。

The problem is that I have a radiogroup containing 4 radiobotton, When selecting a radiobutton I neet to hide some of the editors that are bound to the strong typed propertie fields.

我可以创建一个这样的 javascript 函数

if(radiobonnton1.value = cehcked){
   //Hide not used fields
   //Show used fields
}
else if(radiobonnton2.value = cehcked){
 ...
}
...

问题在于它是一个大函数,如果隐藏了无效的编辑器,我不确定 MVC 验证将如何处理它?还能提交吗?

这真的是要走的路吗?

致以最诚挚的问候

【问题讨论】:

    标签: javascript jquery html model-view-controller


    【解决方案1】:

    我现在已经添加了这个 javascript 方法:

    $(document).ready(function () {
     $("#ModelViewAd\\.TypeOfAd :radio").change(function () {
    
    
                        if (this.id.match('TypeOfAd_Sell$') != null) {
    
                            $("#ModelViewAd_BuyNowPrice").removeAttr('disabled');
                            $("#divBuyNowPrice").fadeIn(500, null);
    
                            $("#divReservationPrice").fadeOut(500, null);
                            $("#ModelViewAd_ReservationPrice").attr('disabled', 'disabled');
    
                            $("#divEndDate").fadeOut(500, null);
                            $("#ModelViewAd_EndDate").attr('disabled', 'disabled');
    
                            $("#divStartingPrice").fadeOut(500, null);
                            $("#ModelViewAd_StartingPrice").attr('disabled', 'disabled');
    
                        }
                        else if (this.id.match('TypeOfAd_Buy$')) {
    
                            $("#divReservationPrice").fadeOut(500, null);
                            $("#ModelViewAd_ReservationPrice").attr('disabled', 'disabled');
    
                            $("#divEndDate").fadeOut(500, null);
                            $("#ModelViewAd_EndDate").attr('disabled', 'disabled');
    
                            $("#divStartingPrice").fadeOut(500, null);
                            $("#ModelViewAd_StartingPrice").attr('disabled', 'disabled');
    
                            $("#divBuyNowPrice").fadeOut(500, null);
                            $("#ModelViewAd_BuyNowPrice").attr('disabled', 'disabled');
    
                        }
                        else if (this.id.match('TypeOfAd_Let$')) {
    
                            $("#ModelViewAd_BuyNowPrice").removeAttr('disabled');
                            $("#divBuyNowPrice").fadeIn(500, null);
    
                            $("#divReservationPrice").fadeOut(500, null);
                            $("#ModelViewAd_ReservationPrice").attr('disabled', 'disabled');
    
                            $("#divEndDate").fadeOut(500, null);
                            $("#ModelViewAd_EndDate").attr('disabled', 'disabled');
    
                            $("#divStartingPrice").fadeOut(500, null);
                            $("#ModelViewAd_StartingPrice").attr('disabled', 'disabled');
    
                        }
                        else if (this.id.match('TypeOfAd_WishRent$')) {
    
                            $("#divBuyNowPrice").fadeOut(500, null);
                            $("#ModelViewAd_BuyNowPrice").attr('disabled', 'disabled');
    
                            $("#divReservationPrice").fadeOut(500, null);
                            $("#ModelViewAd_ReservationPrice").attr('disabled', 'disabled');
    
                            $("#divEndDate").fadeOut(500, null);
                            $("#ModelViewAd_EndDate").attr('disabled', 'disabled');
    
                            $("#divStartingPrice").fadeOut(500, null);
                            $("#ModelViewAd_StartingPrice").attr('disabled', 'disabled');
    
                        }
                        else if (this.id.match('TypeOfAd_Swap$')) {
    
                            $("#divBuyNowPrice").fadeOut(500, null);
                            $("#ModelViewAd_BuyNowPrice").attr('disabled', 'disabled');
    
                            $("#divReservationPrice").fadeOut(500, null);
                            $("#ModelViewAd_ReservationPrice").attr('disabled', 'disabled');
    
                            $("#divEndDate").fadeOut(500, null);
                            $("#ModelViewAd_EndDate").attr('disabled', 'disabled');
    
                            $("#divStartingPrice").fadeOut(500, null);
                            $("#ModelViewAd_StartingPrice").attr('disabled', 'disabled');
    
                        }
                        else if (this.id.match('TypeOfAd_Auktion$')) {
    
                            $("#ModelViewAd_BuyNowPrice").removeAttr('disabled');
                            $("#divBuyNowPrice").fadeIn(500, null);
    
                            $("#ModelViewAd_ReservationPrice").removeAttr('disabled');
                            $("#divReservationPrice").fadeIn(500, null);
    
                            $("#ModelViewAd_EndDate").removeAttr('disabled');
                            $("#divEndDate").fadeIn(500, null);
    
                            $("#ModelViewAd_StartingPrice").removeAttr('disabled');
                            $("#divStartingPrice").fadeIn(500, null);
    
                        }
    
    
                })
    
    
            });
    

    在从服务器到客户端的验证失败时正确设置了单选按钮,但在手动更改单选按钮之前不会运行 javascript?

    【讨论】:

    • 将当前在您的更改处理程序 $("#ModelViewAd\\.TypeOfAd :radio").change(function () { ... }) 中的所有内容移动到一个新函数中。然后从更改处理程序和 jQuery 就绪函数内部调用该函数。
    猜你喜欢
    • 1970-01-01
    • 2020-04-19
    • 2011-03-27
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-19
    相关资源
    最近更新 更多