1

In my form i have a chekbox and when i click the checkbox a textbox is enabled. When i submit the form i got both values(checkbox and textbox)and the site refresh,after the refresh the checkbox is checked but the textbox is disabled but has the last insert value.

If i submit again without making something the textbox returns null and not the last insert value.

I want that the checkbox got the value from my model and change the behavior of the textbox disabled-property und if no new value will be insert,it should return the last insert value.

When i set the model property of false the checkbox is also checked. Has anyone a idea what i can do ?

View:

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

Model:

public bool FilterOrgNameIsCheckedValue { get; set; }

Controller (Function which got called by submit):

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

  • Do not attempt to set the `checked` attribute. The `checked attribute is set automatically based on the value of `FilterModel.FilterOrgNameIsCheckedValue` - if its `true`, then it will be checked, otherwise it will be unchecked. –  May 17 '16 at 08:32
  • And generating `checked="checked"` and `checked="unchecked"` are exactly the same thing - both set the checkbox to checked (except that ` `checked="unchecked"` is invalid html) –  May 17 '16 at 08:34
  • Thanks but the textbox is furthermore disabled after refreshing –  May 17 '16 at 08:35
  • Because you need to run the script when the page is loaded (or add a conditional attribute to disable the textbox based on the value of `FilterModel.FilterOrgNameIsCheckedValue` –  May 17 '16 at 08:49
  • okay and how i can do that ? –  May 17 '16 at 08:50
  • Sorry, need to sign off for a couple of hours. Will add an answer then if no one else does. –  May 17 '16 at 08:51
  • Okay, thank you for helping. –  May 17 '16 at 08:55

1 Answers1

1

Simple Solution ;)

I added a simple if statement in the View:

<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>

I guess its not the best way but it works =)

  • What your doing is just fine (although you could also use a conditional attributes for the textbox (see the first code snippet of [this answer](http://stackoverflow.com/questions/34889537/conditional-html-attribute-with-html-helper/34889685#34889685) or javascript - `$('#OrganisatioName).prop('disabled', !$('#OrgNameChecked').is(':checked'));` –  May 17 '16 at 12:28
  • Cool =) Thank you so much for helping =) –  May 17 '16 at 13:23