Click here to Skip to main content
15,903,728 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
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

What I have tried:

<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; }
    }
Posted
Updated 9-Jul-23 13:08pm

1 solution

 
Share this answer
 
Comments
ahmed_sa 9-Jul-23 19:27pm    
please provide to me full answer


--Select--
@foreach (var printer in Model.PrinterList)
{

@printer.PrinterName


}



on JavaScript i do as below :

var selectElement = document.getElementById("PrinterName-select");
selectElement.addEventListener("change", function () {
var selectedOption = selectElement.options[selectElement.selectedIndex];
var selectedText = selectedOption.text;
var selectedId = selectedOption.id;
console.log("Selected Text: " + selectedText);
console.log("Selected ID: " + selectedId);
on page model two properties display as null value so How to solve this issue please

public ActionResult OnGetSelectedPrinter(string selectedtext,string selectedvalues)
{
string a = SelectedOptionValue;//display as null
string b = SelectedOptionText;//display as null
}
Graeme_Grant 9-Jul-23 19:35pm    
This is a dupelicate question as pointed out above.

You have asked over 465 questions. You should know this already. Programming is writing your own code, not getting others to write it for you. You don't go to the doctor for the doctor to say "hold on, I need to call a friend" every time you ask your doctor a question???

The answer in the link shows you how to add hidden fields to a page. You have everything that you need to do it yourself.
Richard Deeming 10-Jul-23 4:00am    
"You have everything that you need to do it yourself."

Good luck with that - Ahmed is an inveterate help vampire! 🤣
Graeme_Grant 10-Jul-23 4:46am    
It's sad really ... He needs to find his passion elsewhere.
ahmed_sa 9-Jul-23 20:01pm    
Answer you provide i input type hidden
and it is exist on my answer
but this is not enough for working

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900