Click here to Skip to main content
14,270,447 members
Rate this:
Please Sign up or sign in to vote.
Dear all,

Your help will be appreciate for this problem i have with asp.net core razor page.
i have a razor page with a select list field. When an item is selected, i want to get the selected value as a find function parameter. The find function must bring back some values from the database to automatically fill some fields in the page.
My problem is how to send the selected value from the form to the page model on one hand and on another hand is how to send back the find values to some fields of the form.

Thanks for your help

What I have tried:

The form with the select list
form method="post">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <input type="hidden" asp-for="Programmation.IdProgr" />
            <div class="form-group">
                <label asp-for="PrgBareme.IdBareme" class="control-label"></label>
                <select asp-for="SelectedIdBareme" class="form-control" asp-items="Model.BaremeList">
                    <option value="">-- Selectionner un article --</option>
                </select>
                <span asp-validation-for="PrgBareme.IdBareme" class="text-danger" />
            </div>

            <div class="form-group">
                <label asp-for="PrgBareme.PrgPrix" class="control-label"></label>
                <input asp-for="PrgBareme.PrgPrix" class="form-control">
            </div>
            <div class="form-group">
                <label asp-for="PrgBareme.PrgRation1" class="control-label"></label>
                <input asp-for="PrgBareme.PrgRation1" class="form-control">
            </div>
            <div class="form-group">
                <label asp-for="PrgBareme.PrgQte1" class="control-label"></label>
                <input asp-for="PrgBareme.PrgQte1" class="form-control">
            </div>
            <div class="form-group">
                <label asp-for="PrgBareme.PrgQte2" class="control-label"></label>
                <input asp-for="PrgBareme.PrgQte2" class="form-control">
            </div>
            <div class="form-group">
                <label asp-for="PrgBareme.NbrePlat1" class="control-label"></label>
                <input asp-for="PrgBareme.NbrePlat1" class="form-control">
            </div>
            <div class="form-group">
                <label asp-for="PrgBareme.PrgQte3" class="control-label"></label>
                <input asp-for="PrgBareme.PrgQte3" class="form-control">
            </div>
            <div class="form-group">
                <label asp-for="PrgBareme.NbrePlat2" class="control-label"></label>
                <input asp-for="PrgBareme.NbrePlat2" class="form-control">
            </div>
            <div class="form-group">
                <label asp-for="PrgBareme.NbreCrtTomate" class="control-label"></label>
                <input asp-for="PrgBareme.NbreCrtTomate" class="form-control">
            </div>
            <div class="form-group">
                <label asp-for="PrgBareme.NbreBidHuile" class="control-label"></label>
                <input asp-for="PrgBareme.NbreBidHuile" class="form-control">
            </div>
            <div class="form-group">
                <label asp-for="PrgBareme.NbreCartSardine" class="control-label"></label>
                <input asp-for="PrgBareme.NbreCartSardine" class="form-control">
            </div>
            <div class="form-group">
                <label asp-for="PrgBareme.PrgUnite" class="control-label"></label>
                <input asp-for="PrgBareme.PrgUnite" class="form-control">
            </div>
            <div class="form-group">
                <label asp-for="PrgBareme.PrgReversement" class="control-label"></label>
                <input asp-for="PrgBareme.PrgReversement" class="form-control">
            </div>
            <!-- <div class="form-group">
                <label asp-for="PrgBareme.PrgEtat" class="control-label"></label>
                <select asp-for="PrgBareme.ProjectStateCode" class="form-control"
                        asp-items="ViewBag.ProjectStateCode"></select>
            </div>-->
            <div class="form-group">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </form>


The page model script
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Kibsa.Data.Manager_Service;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Mvc.Rendering;

namespace Kibsa.Pages.Programmations
{
    public class Programmation_BaremeCreateModel : PageModel
    {
        private readonly db_kibsaContext _context;

        public Programmation_BaremeCreateModel(db_kibsaContext context)
        {
            _context = context;
        } 

        [BindProperty]
        public Programmation Programmation { get; set; }
        [BindProperty]
        public int SelectedIdBareme { get; set; }
        public List<SelectListItem> BaremeList;

        public async Task<IActionResult> OnGet(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }
            Programmation = await _context.Programmation.Include(c => c.PrgBareme).FirstOrDefaultAsync(c => c.IdProgr == id);

            if (Programmation == null)
            {
                return NotFound();
            }
            BaremeList = _context.Bareme.Select(s => new SelectListItem
            {
                Value = s.IdBareme.ToString(),
                Text = s.BarLibelle
            }).ToList();

            // Get the bareme values of the selected value from database
            List<Bareme> lstbareme = new List<Bareme>();
            lstbareme = (from Bareme in _context.Bareme where Bareme.IdBareme == SelectedIdBareme select Bareme).ToList();


            return Page();
        } 

        [BindProperty]
        public PrgBareme PrgBareme { get; set; }

        public async Task<IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return Page();
            }
            PrgBareme.IdProgr = PrgBareme.IdProgr;

            _context.PrgBareme.Add(PrgBareme);
            await _context.SaveChangesAsync();
            return RedirectToPage("./Programmation_Bareme", new { id =Programmation.IdProgr });
        } 
    }
}
Posted
Updated 23-May-19 11:05am
v3
Comments
Gerry Schmitz 23-May-19 19:25pm
   
Methinks you ask too much.

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100