Search code examples
htmlasp.net-corerazor

Fill input field based on another in Razor forms


I have these 2 input text and I need to fill the second one, the disabled one, when the user inputs a valid ID and tabs out of the first one with a patient name that I get from a database

<input type="text" class="form-control" maxlength="8" @onblur="lostFocus" />
<input type="text" class="form-control" id="name" name="Name"  disabled>

@code {
    public void lostFocus()
    {
        Patient c = new Patient();
        c = PatientService.GetPatientByStudyID (id);
        
        //code to fill the disabled Input.
    }
}

Is there a way to do it without JavaScript?


Solution

  • Just create a name property and set this property value with patient Name:

    <input type="text" class="form-control" maxlength="8" @onblur="lostFocus" />
                                                                //add value="@name"
    <input type="text" class="form-control" id="name" name="Name" value="@name" disabled>
    
    @code {
        public string name { get; set; } = "";
        public void lostFocus()
        {
            Patient c = new Patient();
            c = PatientService.GetPatientByStudyID (id);
            
            //code to fill the disabled Input.
            name = c.Name; 
        }
        public class Patient
        {
            public string Name { get; set; }
        }
    }