Capture value in Radio Button before Submit

Kalyan A 355 Reputation points
Oct 7, 2024, 3:11 PM

I like to capture the values of Radio button button Submit and use validate in IF statement.

If(Radio button button value == "CP"")

{label="Find Selling Price"}

namespace MauiAppMCQs.Models
{
    public class ProfitLossPercentEnums
    {
        public enum Calulation {   ProfitPercent, LossPercent }
    }
}
public class CPSPEnums
    {
        public enum Calculation { CP,SP }
    }
@page "/ProfitLossPercent"
@using System.ComponentModel.DataAnnotations;
@using MauiAppMCQs.Models;
@using static MauiAppMCQs.Models.ProfitLossPercentEnums
@using static MauiAppMCQs.Models.CPSPEnums;
<h3>Find Loss or Profit Percentage  </h3>
<PageTitle>ProfirLossPercentage</PageTitle>
<p>
    Profit = Selling price (S.P.) - Cost price (C.P.)
</p>
<p>
    Loss = Cost price (C.P.) - Selling price (S.P.)
</p>
<p>
    Profit percentage (P%) = (Profit / Cost Price) × 100
</p>
<p>
    Loss percentage (L%) = (Loss / Cost price) × 100
</p>
<div >
  
    <fieldset>
        <legend style="color:blue;">Enter Value : @Calculation</legend>
        <fieldset disabled=@isdisabled>
            <InputRadioGroup @bind-Value="Calculation">
                @foreach (var calculationItem1 in Enum.GetValues<Calculation>())
                {
                    <div>
                        <label>
                            <InputRadio Value="calculationItem1" />
                            @calculationItem1
                        </label>
                    </div>
                }
            </InputRadioGroup>
        </fieldset>
       
        <InputNumber @bind-Value="num1"  disabled=@isdisabled />
       
    </fieldset>
  <br >
    <p>@calcx</p>
  <br >
  
    <fieldset disabled=@isdisabled>
        <InputRadioGroup @bind-Value="Calulation">
            @foreach (var calculationItem in Enum.GetValues<Calulation>())
            {
                <div>
                    <label>
                        <InputRadio Value="calculationItem" />
                        @calculationItem
                    </label>
                </div>
            }
        </InputRadioGroup>
    </fieldset>
    <fieldset>
        <legend style="color:blue;">Enter Value : @Calulation</legend>
        <br >
        <InputNumber @bind-Value="num2" disabled=@isdisabled />
        <br >
        <p>%</p>
    </fieldset>
    <br>
    @value
    <br>
    <div>@num3</div>
    <br >
     <button @onclick="Calculate" disabled=@isdisabled>
            Calculate
        </button>
        <br >
    <button @onclick="Clear" >
        Clear
    </button>
    <br />
    <p style="color: red;">
        @errmsg
    </p>
    <br>
    <div style="color: green;">@value </div>
    <br>
    <br>
    <div style="color: green;">@num3 </div>
    <br>
    <br >
    <div style="color: green;">@profitlosstr </div>
    <br>
     
    
    <br>
    <div style="color: green;">@profitlossper </div>
</div>
@code {
    [Required, EnumDataType(typeof(Calulation))]
    public Calulation? Calulation { get; set; } = MauiAppMCQs.Models.ProfitLossPercentEnums.Calulation.ProfitPercent; 
    [Required, EnumDataType(typeof(Calculation))]
    public Calculation? Calculation { get; set; } = MauiAppMCQs.Models.CPSPEnums.Calculation.CP;
    public string skip; public string type;
    private bool isdisabled = false; public string errmsg = ""; public string calcx = ""; public string value = " ";
    public double profitlossper;
    public double profitloss; public string profitlosstr; public string outresult; public double factor;
    public class ProfitLossPercentEnums
    {
        public enum Calulation { ProfitPercent, LossPercent }
    }
    public class SPEnums
    {
        public enum Calculation { CP, SP }
    }
    private ContactModel contactModel = new ContactModel();
    public class ContactModel
    {
        public string PreferredContactMethod { get; set; }
    }
    public int  num2   ; public double num3;
    public int  num1   ;
    private void Calculate()
    {
        if (num1 > 0)
        {
            isdisabled = true;
        }
        if ((num1 <= 0))
        { skip = "Y"; isdisabled = false; errmsg = "Number should be between 1 and 2000 "; }
        if ((num2 <= 0))
        { skip = "Y"; isdisabled = false; errmsg = "Number should be between 1 and 2000 "; }
        if ((num1 > 2000))
        { skip = "Y"; isdisabled = false; errmsg = "Number should be between 1 and 2000 "; }
        if ((num2 > 100))
        { skip = "Y"; isdisabled = false; errmsg = "Number should be between 1 and 100 "; }
        errmsg = "";
        skip = "N";
        factor = Convert.ToDouble(num2) / 100;
        //Profit = Selling price(S.P.) - Cost price(C.P.)
        if (Calulation == MauiAppMCQs.Models.ProfitLossPercentEnums.Calulation.ProfitPercent)
        {
            profitlosstr = "Profit";
            if (Calculation == MauiAppMCQs.Models.CPSPEnums.Calculation.CP)
            {
                value = "Selling Price";
                num3 = Math.Round((num1 * (1 + factor)),2);
                profitlossper = Math.Round((num3 - num1),2);
            }
            if (Calculation == MauiAppMCQs.Models.CPSPEnums.Calculation.SP)
            {
               
                    value = "Cost Price";
                    num3 =  Math.Round((num1 /(1 +factor)),2);
                profitlossper = Math.Round(( num1 - num3),2);
                
                
            }
           
        
        }
        // Loss = Cost price (C.P.) - Selling price (S.P.)
        if (Calulation == MauiAppMCQs.Models.ProfitLossPercentEnums.Calulation.LossPercent)
        {
            profitlosstr = "Loss";
            if (Calculation == MauiAppMCQs.Models.CPSPEnums.Calculation.CP)
            {
                if (num1 > num2)
                {
                    value = "Selling Price";
                    num3 = Math.Round((num1 * (1 - factor)),2);
                    profitlossper = Math.Round((num1 - num3),2);
                }
                
            }
            if (Calculation == MauiAppMCQs.Models.CPSPEnums.Calculation.SP)
            { 
                value = "Cost Price";
                num3 = Math.Round((num1 / (1 - factor)), 2);
                profitlossper =Math.Round(( num3 - num1),2);
            }
        }
    
 
    
    } 
    private void Clear()
    {
        num1 = 0; isdisabled = false; num2 = 0; calcx = ""; num3 = 0.0; value = ""; profitlossper = 0.0;
        profitloss = 0.0; profitlosstr = ""; outresult = "";
    }
}
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,796 questions
{count} votes

Accepted answer
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 46,586 Reputation points Microsoft Vendor
    Oct 9, 2024, 1:55 AM

    Hello,

    You could listen to the value of the RadioButton to change the value of the Label before submitting the form. Please refer to the following minimized code snippet.

    <InputRadioGroup ValueChanged="@((e) => OnRadiochange(e))" TValue="string" ValueExpression="() => SelectedValue">
        @foreach (var item in Items)
        {
            bool Checked = false;
            if (SelectedValue.Equals(item, StringComparison.OrdinalIgnoreCase))
            {
                Checked = true;
            }
    <div class="form-check">
    <InputRadio Value="@item" class="form-check-input" checked=@Checked />
                @item <br />
    </div>
        }
    </InputRadioGroup>
    <label>
        @Price
    </label>
    @code{
        public string SelectedValue { get; set; } = "CP";
        public string Price{ get; set; }
        public List<string> Items { get; set; } = new List<string> { "CP", "NotCp"};
     
        private void OnRadiochange(object sender)
        {
            SelectedValue = (string)sender;
            if (SelectedValue.Equals("CP"))
            {
                Price = "Find Selling Price";
            }
            else
            {
                Price = "Other";
            }
            StateHasChanged();
        }
    }
    

    Update :

    get this error "The delegate type could not be inferred"

    I tested with your code and this error did not appear. This error is usually caused by a data binding error, please check your page for data binding errors.

    I want one of the buttons checked by default.

    InputRadio does not have a Checked property, for InputRadioGroup you need to set the initial value using Value or @bind-value. Please refer to the following code snippet.

    
     
    <InputRadioGroup Value="@SelectedValue" disabled=@isdisabled ValueChanged="@((e) => OnRadiochange(e))" TValue="string" ValueExpression="() => SelectedValue">
    
        @foreach (var item in Items)
    
        {
     
            <div class="form-check">
    <InputRadio Value="@item" class="form-check-input"  />
    
                @item <br />
    </div>
    
        }
    </InputRadioGroup>
     
    
    // set the default value of Price.
    public string Price { get; set; } = "Inverse Proportion Workers and Time Problems";
    

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.