2012-06-13 63 views
2

如何在mvc3控制器中啓用和禁用@html.textbox如何在mvc3控制器中爲@ html.textbox啓用和禁用

我的文本框代碼:

@Html.TextBox("txtIden1") 

點擊按鈕後,我怎麼可以禁用或啓用控制器文本框。

我寫的按鈕單擊事件代碼控制,如下面

@using(Html.BeginForm()) 
{ 
@Html.TextBox("txtCustomerFilter"); 
} 

<button name="button" value="Submit">Save</button>&nbsp; 
<button name="button" value="cancel">Cancel</button> 

控制器:

[AcceptVerbs(HttpVerbs.Post)] 
public ActionResult Customer(string button, 
     customer customerInsert, FormCollection formvalues) 
{ 
    if(button == "Submit") 
    { 
    //Code 
    } 
    else 
    { 
    //Code 
    } 

    return View(); 
} 

回答

3

我會建議你使用瀏覽模式:

public class CustomerViewModel 
{ 
    public string Button { get; set; } 
    public string Filter { get; set; } 
    public bool Disabled { get; set; } 

    ... some other properties that will represent the customer 
     details 
} 

,然後:

public ActionResult Customer() 
{ 
    var model = new CustomerViewModel(); 
    return View(model); 
} 


[HttpPost] 
public ActionResult Customer(CustomerViewModel model) 
{ 
    if(model.Button == "Submit") 
    { 
     model.Disabled = true; 
     //Code 
    } 
    else 
    { 
     //Code 
    } 

    return View(model); 
} 

,然後有一個強類型的視圖:

@model CustomerViewModel 
@using (Html.BeginForm()) 
{ 
    @Html.TextBoxFor(
     x => x.Filter, 
     Model.Disabled ? new { @readonly = readonly } : null 
    ) 

    <button name="button" value="Submit">Save</button> 
    <button name="button" value="cancel">Cancel</button> 
} 
+0

是否可以有一個屬性總是和其他可選?如: '@ Html.TextBoxFor(x => x.AddFirstLine,Model.AutomaticAddress? new {'@class =「form-control」,'@disabled =「disabled」}:new {@class =「 form-control「}) – Juan