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

时间:2012-06-13 05:41:18

标签: asp.net-mvc-3

如何在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();
}

1 个答案:

答案 0 :(得分: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>
}