2

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

我的文本框代码:

@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();
}
4

1 回答 1

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>
}
于 2012-06-13T05:57:37.780 回答