I have the following lines of code in a cshtml file,

<div class="YesNo">
    <label><input type="radio" name="YesNo_@pNum" value="Yes" />@yesLabel</label>
    <label><input type="radio" name="YesNo_@pNum" value="No" checked />@noLabel</label>

How can I use an if condition in Razor to check if "Yes" radiobutton is checked and then only display a certain label?

I have these 2 properties in my model,

public string YesLabel { get { return COff.YesLabel ?? "Yes"; } }
public string NoLabel { get { return COff.NoLabel ?? "No"; } }

I have 2 variables for that as well in the cshtml file,

var yesLabel = off.YesLabel;
var noLabel = off.NoLabel;

I have these 2 porperties in one of my .cs files,

public string YesLabel { get; set; }
public string NoLabel { get; set; }

I used this JS code and it worked but since my "No" radioButton is already checked, I am not able to get the desired output,that is if I select "Yes" after visiting the page, i dont see my code working because "No" is always checked in the memory,

$(function () {                       
    var checkedRadio = $('[name="YesNo_@placementNumber"]:radio:checked').val();
    if (checkedRadio == "Yes") {
        $("#sel").html("Selected Value: " + checkedRadio);

Please any help is appreciated.

Related posts

Recent Viewed