有条件地启用和禁用MVC中的HTML按钮 [英] Conditionally enable and disable a HTML button in MVC

查看:92
本文介绍了有条件地启用和禁用MVC中的HTML按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在MVC 4中启用/禁用html控件,请查看以下代码



我的浏览页面是

< button type =buttonclass =btn btn-primarydata-toggle =modaldata-target =#myModalid =btnAddCompvalue =重置

@ {if(ViewBag.CompAddFlg!= true)

{@:disabled =disabled

}

}

onclick =clearTextBox();>

添加新公司



我的控制器是

if(list.AddFlg == 1)

{

ViewBag.CompAddFlg =true;

}

else

{

ViewBag.CompAddFlg =false;

}


我的尝试:



尊敬的各位同志请查看我的代码让我知道变化....

I want to Enable/Disable a html control in MVC 4, please check with the below code

My View Page is
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal" id="btnAddComp" value="Reset"
@{ if (ViewBag.CompAddFlg != true)
{ @:disabled="disabled"
}
}
onclick="clearTextBox();">
Add New Company

My Controller is
if (list.AddFlg == 1)
{
ViewBag.CompAddFlg = "true";
}
else
{
ViewBag.CompAddFlg = "false";
}

What I have tried:

Dear Comrades please check with my code and let me know the changes....

推荐答案

true - 这是一个字符串

true - 这是一个布尔值



这两种是不同的类型,你无法比较它们。到处使用bool



"true" - this is a string
true - this is a boolean

Those two are different types, you can't compare them. Use bools everywhere

if (list.AddFlg == 1)
{
    ViewBag.CompAddFlg = true;
}
else
{
    ViewBag.CompAddFlg = false;
}


这篇关于有条件地启用和禁用MVC中的HTML按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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