How to store selected Id and selected text on hidden field using razor

ahmedaziz

Well-known member
Joined
Feb 22, 2023
Messages
55
Programming Experience
1-3
I work on razor asp.net core . I face issue i can't store selected value id and selected text of select option drop down on hidden fields

so I need to create two hidden fields first hidden field to store selected value id and second for store selected text

How to store selected text and selected id on two hidden fields:
<div class="col-md-3 col-lg-2">
                        <label for="printerserver-selectlbl" style="margin-left:3px;font-size:15px;font-family: 'Open Sans', sans-serif;font-weight: bold;">Print Location</label>

                        <select id="PrinterName-select" asp-for="SelectedPrinterId" name="selectprinterid" class="form-select" style="margin-left:3px;font-size:15px;font-family: 'Open Sans' , sans-serif;font-weight: bold;">
                            <option value="0">--Select--</option>
                            @foreach (var printer in Model.PrinterList)
                            {

                                <option value="@printer.UserPC">@printer.PrinterName</option>


                            }
                        </select>

                    </div>

public class LabelPrintingModel : PageModel
    {


        [BindProperty]
        public List<LabelPrinitingView> PrinterList { get; set; }

public async void OnGet()
{
 PrinterList = dtprinters.AsEnumerable()
 .Select(row => new LabelPrinitingView
 {
     // Map the values from the DataRow to the properties of the PrintServer object
     UserPC = (string)row["UserPC"],
     PrinterName = row["PrinterName"].ToString()
     // etc.
 })
 .ToList();
}

 public class LabelPrinitingView
    {
        public string UserPC { get; set; }
        public string PrinterName { get; set; }
    }
 
That's because you would need to the storing into hidden fields using JavaScript on the client side, not in C# within the Razor.
 
No. Because as I said, you can't use C# in Razor to update the values of the hidden fields. Razor is on the server side. The Razor page is converted into a HTML page which is then sent to the browser on the client side. So yes you can set the hidden fields before the conversion to HTML. This is no different than setting non-hidden fields. Once the Razor pages is converted to HTML and sent to the client side browser, updates to the hidden fields will have to be performed on the browser side using JavaScript (or web assembly, or ActiveX controls, etc.). That seems to be your goal. That's a JavaScript question, not a C# question at that point. This is a C# forum.
 

Latest posts

Back
Top Bottom