Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Taymar 9 posts 29 karma points
    Jun 23, 2012 @ 21:41
    Taymar
    0

    How to separate radio group ID's in razor? (cultiv contactform)

    I've been working with the cultiv contactform plugin, and I've got it mostly adjusted to my purposes. I've been stuck on this item for hours though.

    It uses this helper method to render a textbox:

     

    @helper RenderTextField(string fieldLabel, string fieldName, string errorText, string className)

    {

        <div class="row">

            <label for="@fieldName">@fieldLabel</label>

            <input type="text" id="@fieldName" name="@fieldName" class="default @className @(Page.Errors.Contains(fieldName) ? "error" : "")" value="@HttpContext.Current.Request[fieldName]" />

            @if (Page.Errors.Contains(fieldName))

            {

                <span class="error">@errorText</span>

            }

        </div>

    }

    so I've tried to modify it to use checkboxes as follows:

     

    @helper RenderRadioField(string fieldLabel, string fieldName, string errorText, string className)

    {

        <div class="row">

            <label for="@fieldName">@fieldLabel</label>

     

            <input type="radio" id="@fieldName" name="@fieldName" class="default @className @(Page.Errors.Contains(fieldName) ? "error" : "")" value="yes">

            <input type="radio" id="@fieldName" name="@fieldName" class="default @className @(Page.Errors.Contains(fieldName) ? "error" : "")" value="no">

     

            @if (Page.Errors.Contains(fieldName))

            {

                <span class="error">@errorText</span>

            }

        </div>

    }

    However I believe I can't use the same ID's for both radio buttons, but if I change one it gets ignored. The closest I've got is to remove one of the radio buttons and then it works, but I really would like a yes/no option (and be able to access the value as yes/no if possible). I have added a dropdown list but that was straightforward since all the razor code goes on the select not the options.

    If anyone could give me some suggestions it would be a huge help.

     

     

     

     

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Jun 24, 2012 @ 04:53
    Tom Fulton
    1

    Hi,

    You're right that you shouldn't use the same ID for each radio button, however I think you should leave the name attribute the same on both.  Then when you do Request.Form["name"] you'll get either "yes" or "no" (whichever one is selected during the submit).

    Hope this helps,
    Tom 

Please Sign in or register to post replies

Write your reply to:

Draft