如何在mvc3中的控制器中为@ html.textbox启用和禁用. [英] how to give enable and disable for @html.textbox in controller in mvc3.

查看:69
本文介绍了如何在mvc3中的控制器中为@ html.textbox启用和禁用.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在mvc3的控制器中为@ html.textbox启用和禁用.

我的文本框代码:

 @ Html.TextBox("txtIden1")



谢谢.

解决方案

disable属性不是true \ false,但应该读为disable .... e.g

 @ Html.TextBox(" "  已禁用"})




您无需在控制器中进行设置,控制器负责将模型返回到指示应如何呈现的视图.因此,在您的控制器方法中

 // 您的模型
公共  SomeModel
{
    公共 字符串 SomeName { get ; ;}
    公共 布尔 FieldIsDisabled { get ; ;}
}

// 您的控制器操作
公共 ActionResult GetMyModel()
{
     var  model =  SomeModel
    {
        SomeName = " ,
        FieldIsDiabled =  true  // 实际上是确定逻辑这个... 
    }

   返回视图(模型);
}

// 您的视图页面

@model SomeModel
@ {
    ViewBag.Title = " ;
}


@ Html.TextBoxFor(model = >  model.SomeName,model.FieldIsDiabled?( object ) {disabled = " }: {})


看看以下内容可能对您有帮助:
http://stackoverflow.com/questions/6660146/set-disable-attribute-based-on-a-condition-for-html-textboxfor [ http://stackoverflow.com/questions/9086104/mvc3-conditionally-disable-html-textboxfor [ ^ ]
http://stackoverflow. com/questions/11008831/how-to-give-enable-and-disable-for-html-textbox-in-controller-in-mvc3 [ ^ ]

How to give enable and disable for @html.textbox in controller in mvc3.

My textbox code:

@Html.TextBox("txtIden1")



Thanks.

解决方案

The disable property isn''t true \ false, but should just read as disabled....e.g

@Html.TextBox("txtIden1", new { disabled="disabled" })




You don''t set this in a controller, your controller is responsible for returning a model to the view that dictates how it should be rendered. So, in your controller method

// Your model
public class SomeModel 
{
    public string SomeName {get; set;}
    public bool FieldIsDisabled {get; set;}    
}

// Your controller action
public ActionResult GetMyModel()
{
    var model = new SomeModel
    {
        SomeName = "Test", 
        FieldIsDiabled = true // would actually be some logic to determine this...
    }

   return View(model);
}

// Your view page

@model SomeModel
@{
    ViewBag.Title = "Home Page";
}


@Html.TextBoxFor(model => model.SomeName, model.FieldIsDiabled ? (object)new { disabled="disabled" } : new {})


have a look at the following these might help you :
http://stackoverflow.com/questions/6660146/set-disable-attribute-based-on-a-condition-for-html-textboxfor[^]

http://stackoverflow.com/questions/9086104/mvc3-conditionally-disable-html-textboxfor[^]
http://stackoverflow.com/questions/11008831/how-to-give-enable-and-disable-for-html-textbox-in-controller-in-mvc3[^]


这篇关于如何在mvc3中的控制器中为@ html.textbox启用和禁用.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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