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

Ahmed Abd El Aziz 315 Reputation points
2023-07-09T20:40:29.46+00:00

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; }
    }
Developer technologies | ASP.NET | ASP.NET Core
Developer technologies | ASP.NET | Other
{count} votes

4 answers

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  2. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  3. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  4. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2023-07-10T20:13:51.0633333+00:00

    Below I present a select with a label were in this case the select is populated using the following model.

    public class Category
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public override string ToString() => Name;
    
    }
    

    select

    Index page with the above select

    public class IndexModel : PageModel
    {
    
        public List<SelectListItem> Options { get; set; }
        [BindProperty]
        public int SelectedCategory { get; set; }
        private readonly IOptions<List<Category>> _categories;
        public IndexModel(IOptions<List<Category>> categories)
        {
            _categories = categories;
    
        }
    
        public void OnGet()
        {
            Options = _categories.Value.Select(category => new SelectListItem()
            {
                Value = category.Id.ToString(),
                Text = category.Name
            }).ToList();
        }
    
        public RedirectToPageResult OnPost(int id)
        {
    
            return RedirectToPage("Results", new
            {
                sender = _categories.Value.FirstOrDefault(x => x.Id == id)!.Name
            });
        }
    }
    

    Index front end code

    <div class="container">
        
        <div class="alert alert-primary shadow mt-2 mb-4" role="alert">
            <h1 class="fs-6 text-center">Reading list from settings</h1>
        </div>
    
        <form id="main" method="post">
            
            <div class="input-group">
    
                <label asp-for="SelectedCategory">Categories
                    <select name="Id"
                            asp-for="SelectedCategory"
                            class="form-select"
                            style="width: 9.2em"
                            asp-items="Model.Options">
                    </select>
                </label>
    
                <input type="submit"
                       value="Save"
                       class="btn btn-primary ms-2"/>
            </div>
            
        </form>
    </div>
    

    OnPost we direct to Results page

    @page
    @model ReadListApplication.Pages.ResultsModel
    @{
    }
    <div class="alert alert-primary shadow mt-2 mb-4" role="alert">
        <h1 class="fs-6 text-center">Posted from Index page</h1>
    </div>
    Selected Category <span class="text-danger">@Model.CategoryName</span>
    

    Back end

    public class ResultsModel : PageModel
    {
        [BindProperty]
        public string CategoryName { get; set; } = string.Empty;
        public void OnGet(string sender)
        {
            CategoryName = sender;
        }
    }
    

    Full source https://github.com/karenpayneoregon/razor-pages-IOptions-samples/tree/master/ReadListApplication

    0 comments No comments

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.