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.
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).
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.
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
is working on a reply...