DmitryEfimenko / TwitterBootstrapMvc

Fluent implementation of ASP.NET-MVC HTML helpers for Twitter Bootstrap.
Apache License 2.0
224 stars 79 forks source link

VisibleDependsOn #439

Open vanithav opened 7 years ago

vanithav commented 7 years ago

VisibleDependsOn is not working. I am using the js version 3.0.4 downloaded from site. Is that the latest version? Is there any other depedency on jquery related files?

VisibleDependsOn(m => m.OtherExplanation, "Y")

If i have to specify nullable bool for VisibleDependsOn, is that possible?

DmitryEfimenko commented 7 years ago

you are using an old version. Get latest using Nuget (I should remove download links from the website as I do not maintain them). You can also see the what's latest version using Nuget

BMVC provides an additional javascript file. It gets automatically installed into scripts folder when you download BMVC using Nuget.

vanithav commented 7 years ago

I installed using nuget and this is the version I have (TwitterBootstrapMVC5.3.17.1). Since that was not working I downloaded from the site. Again I replaced the js file from the one in package. It still does not work.

From: Dmitry A. Efimenko [mailto:notifications@github.com] Sent: Thursday, November 17, 2016 10:30 PM To: DmitryEfimenko/TwitterBootstrapMvc Cc: Vanitha Yoganand; Author Subject: Re: [DmitryEfimenko/TwitterBootstrapMvc] VisibleDependsOn (#439)

you are using an old version. Get latest using Nuget (I should remove download links from the website as I do not maintain them). You can also see the what's latest version using Nuget https://www.nuget.org/packages/TwitterBootstrapMVC/

BMVC provides an additional javascript file. It gets automatically installed into scripts folder when you download BMVC using Nuget.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/DmitryEfimenko/TwitterBootstrapMvc/issues/439#issuecomment-261303898 , or mute the thread https://github.com/notifications/unsubscribe-auth/AFGLHhgomKQnYC8WNRKc5dpfFKuGfaerks5q_If0gaJpZM4K1JjJ .Image removed by sender.


No virus found in this message. Checked by AVG - www.avg.com Version: 2016.0.7859 / Virus Database: 4664/13428 - Release Date: 11/17/16

vanithav commented 7 years ago

I have a yes/no radio button. The dropdown visibility is based on this. Should be visible when Yes is selected.

`

                    <div class='radio checkbox-inline' style="vertical-align:bottom;"><label for="IsMaterialGradeIncorrect_1">@Html.RadioButtonFor(m => m.IsMaterialGradeIncorrect, false)No</label></div>
                </div>`

@f.FormGroup().DropDownListFor(m => m.MaterialGradeId, Model.MaterialGrades).VisibleDependsOn(m => m.IsMaterialGradeIncorrect, "True").WidthLg(4).HtmlAttributes(new { @class = "input-sm" }).OptionLabel(" ").Label().WidthLg(3).ShowRequiredStar(false)

DmitryEfimenko commented 7 years ago

Just checked. Similar code works for me. Make sure that file TwitterBootstrapMvcJs.js is actually referenced in your code and is being run. To see if it's being run just put a console.log('hit') in it and see if hit is displayed in browser console when site runs

kfinke70 commented 7 years ago

This is working for me if I use a visual element such as a Checkbox. However, what if I just wanted this functionality based on a bool in the model? The fields obviously still need to be present in the form, but there are conditions where I'd like to hide some fields from the user. They would either have to be conditionally written as input type hidden, or take the FormGroup() and set it's display. I thought perhaps this would be a clean solution, but it doesn't appear to work.

DmitryEfimenko commented 7 years ago

Could you show me some code of what you are trying to achieve? Please format the code snippet when submitting

kfinke70 commented 7 years ago

Sure, I'd love to write something like: @(f.FormGroup().Class("label-floating").DisplayIf(m => m.SerialNumber.IsNullOrWhiteSpace()) .TextBoxFor(m => m.SerialNumber) .Append(Html.ValidationMessageFor(m => m.SerialNumber))

So far I have something like:

`        public static BootstrapControlGroupBase<TModel> DisplayIf<TModel>(
            this BootstrapControlGroupBase<TModel> control, Expression<Func<TModel, bool>> expression)
        {
            bool condition = ModelMetadata.FromLambdaExpression<TModel, bool>(expression, control._model);
            if (condition)
            control.HtmlAttributes(new {style = "display:none;"});

            return control;
        }`

but I can't figure out how to satisfy FromLambdaExpression. _model is not reachable here.

DmitryEfimenko commented 7 years ago

yes, the _model is an internal property. I can make it public, but before I do so, I'd like to make sure you understand that this approach only will work on the page load because that's just a server side code running. It's much easier and probably better to just write something like this:

@{
    if(String.IsNullOrWhiteSpace(Model.SerialNumber)) {
        @(f.FormGroup().Class("label-floating").TextBoxFor(m => m.SerialNumber).Append(Html.ValidationMessageFor(m => m.SerialNumber))
    }
}
kfinke70 commented 7 years ago

Yes, I understand. What your suggesting would not write the section to the page at all. My purpose here is to hide a few fields that the user doesn't need to see unless they haven't provided them already. However, I still need them on the form so they post up on the save.

I ended up passing in the ViewData myself, so I have something like:

        public static BootstrapControlGroupBase<TModel> DisplayIf<TModel>(
            this BootstrapControlGroupBase<TModel> control, Expression<Func<TModel, bool>> expression, ViewDataDictionary<TModel> viewData)
        {
            bool condition = (bool) ModelMetadata.FromLambdaExpression<TModel, bool>(expression, viewData).Model;
            control.HtmlAttributes(new {style = "display:none;"});

            return control;
        }

Looks like: @f.FormGroup().DisplayIf(m => m.ShowFirmAddress, ViewData).EditorFor(m => m.FirmAddress)

Just exposing _model seems heavy handed, but giving a way to reach something that allows for the extension method to be able to get the Model data would be awesome. Just a thought.

DmitryEfimenko commented 7 years ago

Sounds good, but I'd personally still use an if else statement:

@{
    if(String.IsNullOrWhiteSpace(Model.SerialNumber)) {
        @(f.FormGroup().Class("label-floating").TextBoxFor(m => m.SerialNumber).Append(Html.ValidationMessageFor(m => m.SerialNumber))
    }
    else
    {
        @Html.hiddenFor(m => m.SerialNumber)
    }
}

This would post the value to the server if it's present.

DmitryEfimenko commented 7 years ago

Though using your extension looks cleaner of course