ahmed salah

ahmed salah

  • 1.2k
  • 547
  • 64.7k

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

Jul 9 2023 8:34 PM

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 

<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; }
    }

 


Answers (2)