How create UIHit YesNoNull?

2019-09-14 22:18发布

问题:

I need create UIHint to be able to connect it to any type, and it worked: If the checkbox is selected the data are visible if the checkbox is not selected then the data should be hidden and passed null.

But @Html.EditorForModel() not worked.

In model:

[UIHint("_YesNoNull")]
[DisplayName("Ссылка на место проведения")]
public string VenueUrl { get; set; }

_YesNoNull control:

@{
    Guid guid = Guid.NewGuid();
    var propertyName = this.ViewData.ModelMetadata.PropertyName;
}
<script type="text/javascript">
    $(function () {
        $('#@(propertyName)_checkbox').click(function () {

            var checked = $(this).attr('checked');
            if (checked != undefined) {
                $(this).val(true);
            }
            else {
                $(this).val(false);
            }
        });
    });
</script>
@Html.CheckBox("checkbox", false)
@Html.EditorForModel()//not worked

source html:

<div class="editor-label">
            <label for="VenueUrl">Ссылка на место проведения</label>
        </div>
        <div class="editor-field">
            <script type="text/javascript">
    $(function () {
        $('#VenueUrl_checkbox').click(function () {

            var checked = $(this).attr('checked');
            if (checked != undefined) {
                //show VenueUrl
            }
            else {
                //hide VenueUrl
            }
        });
    });
</script>
<input id="VenueUrl_checkbox" name="VenueUrl.checkbox" type="checkbox" value="true" /><input name="VenueUrl.checkbox" type="hidden" value="false" />


            <span class="field-validation-valid" data-valmsg-for="VenueUrl" data-valmsg-replace="true"></span>
</div>

what I want to get a result:

   <div class="editor-label">
                <label for="VenueUrl">Ссылка на место проведения</label>
            </div>
            <div class="editor-field">
                <script type="text/javascript">
        $(function () {
            $('#VenueUrl_checkbox').click(function () {

                var checked = $(this).attr('checked');
                if (checked != undefined) {
                    //show VenueUrl
                }
                else {
                    //hide VenueUrl
                }
            });
        });
    </script>
    <input id="VenueUrl_checkbox" name="VenueUrl.checkbox" type="checkbox" value="true" /><input name="VenueUrl.checkbox" type="hidden" value="false" />

<div class="editor-field">

<input class="text-box single-line" id="VenueUrl" name="VenueUrl" type="text" value=""/>

</div>
    <span class="field-validation-valid" data-valmsg-for="VenueUrl" data-valmsg-replace="true"></span>
</div>

回答1:

Instead of

@Html.EditorForModel()//not worked

use

@Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue)