C#asp.net mvc使用Model-Value设置CheckBoxFor checked-property

时间:2016-05-17 08:29:08

标签: javascript c# asp.net asp.net-mvc checkbox

在我的表格中我有一个chekbox,当我点击复选框时,启用了一个文本框。 当我提交表单时,我得到了两个值(复选框和文本框)和网站刷新,刷新后复选框被选中,但文本框被禁用但具有最后一个插入值。

如果我再次提交而没有做任何事情,则文本框返回null而不是最后一个插入值。

我希望复选框从我的模型中获取值并更改textbox disabled-property und的行为,如果没有插入新值,它应该返回最后一个插入值。

当我将model属性设置为false时,也会选中复选框。 有谁知道我能做什么?

查看:

@Html.TextBoxFor(m => m.FilterModel.FilterOrgNameValue, new { @class = "form-control", @id = "OrganisatioName", @disabled = "disabled" })
@Html.CheckBoxFor(m => m.FilterModel.FilterOrgNameIsCheckedValue, new { @class = "form-control", @id = "OrgNameChecked", @checked = (Model.FilterModel.FilterOrgNameIsCheckedValue ? "checked" : "unchecked")})

JavaScript的:

 $("#OrgNameChecked").click(function () {

            $('#OrganisatioName').attr("disabled", $(this).is(':unchecked'));
        });

型号:

public bool FilterOrgNameIsCheckedValue { get; set; }

控制器(通过提交调用的功能):

public ActionResult Index(AdminOrganizationModel m)
        {
            AdminOrganizationModel model = new AdminOrganizationModel();
            if(TempData["model"] != null)
            {
                m = (AdminOrganizationModel)TempData["model"];
            }
            if(m.FilterModel == null)
            {
                m.FilterModel = new AdminOrganizationFilterModel();
            }
            model = m;
            OrganizationBusinessObject organizationBusinessObject = new OrganizationBusinessObject(DbContext);
            var organizations = DbContext.GetAllEntitiesQueryable<Organization>();
            organizations = FilterOrganizations(organizations, model); 
            InitializeLicenseList(1);
            AdminOrganizationModelBuilder modelBuilder = new AdminOrganizationModelBuilder();
            IList<AdminOrganizationModel> organizationsModels = modelBuilder.GetModelCollection(organizations);
            model.Organizations = new AjaxGridFactory().CreateAjaxGrid(organizationsModels.AsQueryable(), 1, false, 10) as AjaxGrid<AdminOrganizationModel>;

            return View(model);
        }

Fields after submit

1 个答案:

答案 0 :(得分:1)

简单解决方案;)

我在视图中添加了一个简单的if语句:

<div class="col-sm-2">
@if (Model.FilterModel.FilterOrgNameIsCheckedValue)
{
    @Html.TextBoxFor(m => m.FilterModel.FilterOrgNameValue, new { @class = "form-control", @id = "OrganisatioName"})
}
else
{
    @Html.TextBoxFor(m => m.FilterModel.FilterOrgNameValue, new { @class = "form-control", @id = "OrganisatioName", @disabled = "disabled" })
}
 </div>

我想这不是最好的方法,但它有效=)