user3883648
user3883648

Reputation:

C# asp.net mvc Set CheckBoxFor checked-property with Model-Value

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

Upvotes: 1

Views: 1886

Answers (1)

user3883648
user3883648

Reputation:

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

Upvotes: 1

Related Questions