0

I have this model

    public partial class PRB
{
    public long PRB_ID { get; set; }
    public string MEMBERSHIP_NUMBER { get; set; }
    public Nullable<int> MEMBERSHIP_TYPE { get; set; }
    public string REEGISTERED_BUSINESS_NAME { get; set; }
}

I want to make MEMBERSHIP_TYPE to be a radiobutton

                                                    <div class="form-group">
                                                    <div class="radio">
                                                        @Html.RadioButtonFor(m => m.MEMBERSHIP_TYPE, 1, new { id = "", @checked = "checked" }) Foreign Company
                                                    </div>
                                                    <div class="radio">
                                                        @Html.RadioButtonFor(m => m.MEMBERSHIP_TYPE, 2, new { id = "" }) Foreign Owned Nigerian Company
                                                    </div>
                                                    <div class="radio">
                                                        @Html.RadioButtonFor(m => m.MEMBERSHIP_TYPE, 3, new { id = "" }) Nigerian Company
                                                    </div>
                       </div>
  1. If radiobutton MEMBERSHIP_TYPE that is clicked is 1, the message box displayed will be "You are a Grade A member". Then OK button will be clicked
  2. If radiobutton MEMBERSHIP_TYPE that is clicked is 2, the message box displayed will be "You are a Grade B member". Then OK button will be clicked
  3. If radiobutton MEMBERSHIP_TYPE that is clicked is 3, the message box displayed will be "You are a Grade C member". Then OK button will be clicked

Then, after the click of OK button for the message box, it will diplay what is shown below.

        <div class="form-group">
        @Html.LabelFor(model => model.REGISTERED_BUSINESS_NAME, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.REGISTERED_BUSINESS_NAME)
            @Html.ValidationMessageFor(model => model.REGISTERED_BUSINESS_NAME)
        </div>
    </div>

Then the user will enter the Business name textbox.

Please help.

Ayo Idowu
  • 15
  • 3
  • So what have you tried and where are you stuck? This isn't a question and should be closed until you can come up with something more than a plea for someone else to do your work. – Travis Acton May 23 '17 at 16:11
  • Sorry. Am not saying you should do my work. I've already developed the multi-step wizard, radiobuttons and other controls. The issue is how to make each of my radiobutton to display a message when clicked before going to the next step. I dont know how to do this – Ayo Idowu May 23 '17 at 16:45
  • 1. Take away the "id" property element from your radio buttons. That will be automatically generated when using radiobuttonfor. 2. Add a class to each radio button, something like "radioMem" 3. Add a dialog modal to the page in hidden state 4. Set your business name form group to hiddens tate 5. Use JQuery to bind to the on change event of the "radioMem" class on doc ready. It should contain a case for the value of the radio selected in which you can populate and show the html inside your modal. 6. Use JQuery to bind on the modal hidden event to then display your reg bus name form group. – Travis Acton May 23 '17 at 17:17
  • @TravisActon, Do not remove `new { id = "" }` - without that, the `RadioButtonFor()` method generates 3 inputs with `id="MEMBERSHIP_TYPE"` which is invalid html. –  May 23 '17 at 22:41
  • @Stephen-Muecke thanks, forgot about that little gotcha with the HTML helper for the radio btn – Travis Acton May 24 '17 at 02:08

1 Answers1

0

If your project includes jQuery, you can show message box when changing radio button selection using this simple code:

$('input[type=radio]').change(function () {
    // radio button selection part
    var value = parseInt($(this).val());

    switch (value) {
        case 1:
           alert("You are a Grade A member");
           break;
        case 2:
           alert("You are a Grade B member");
           break;
        case 3:
           alert("You are a Grade C member");
           break;
        default:
           // do something else
           break;
    }

    // code to show/hide div or using modal popup here
});

Tips:

  1. Use $(this).is(':checked') to check current value of radio button group(s).

  2. If your radio button controls is more than given above, consider assign a class for corresponding radio buttons e.g. @Html.RadioButtonFor(m => m.MEMBERSHIP_TYPE, 1, new { id = "", @class = "classname" }) and set jQuery selector pointed to that CSS class such as $('.classname').change(function () { ... }).

Depending on what you need, EditorFor part can be presented as simple div wrapper or modal popup. If the former has used, give a value for id attribute to that div element and use hide()/show() method as toggle, like this example:

HTML

<div id="regname" class="form-group">
    @Html.LabelFor(model => model.REGISTERED_BUSINESS_NAME, new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.EditorFor(model => model.REGISTERED_BUSINESS_NAME)
        @Html.ValidationMessageFor(model => model.REGISTERED_BUSINESS_NAME)
    </div>
</div>

JS

$(document).ready(function () {
     $('#regname').hide();

     $('input[type=radio]').change(function () { 
         // see radio button selection code above

         // show target div
         $('#regname').show();
     });
});

If the latter one is used, follow same procedure to give id attribute on div element (see also: "When creating a dialog with jquery, how do I hide the dialog div?"), then use dialog method like this example instead:

$(document).ready(function () {
    $('#regname').dialog({
        autoOpen: false, 
        modal: true
        // other property settings here
    });

    $('input[type=radio]').change(function () { 
         // see radio button selection code above

         // show target modal popup
         $('#regname').dialog("open");
    });
});

This is not a perfect explanation, but at least explain what should you do to show message box & displaying other part of view when changing radio button selection.

Tetsuya Yamamoto
  • 24,297
  • 8
  • 39
  • 61