简体   繁体   中英

in dot net core razor pages - how do you default a dropdown choice on a create page?

I current have parent page that properly navigates to a different child page. The Child page has the stand grid of child records- AND it also has 'Create' link to add a new child record.

Under standard Entity Framework Scaffolding the 'Create' page has standard html'dropdown' lists for the fields that are linked to 'lookup' values.

Below are some screen shots:

父屏幕

The Position Details link navigates to the child records: 子网格

The Create New Link brings up the standard scaffolded create page: 在此处输入图像描述

The PositionId field shows the full list of choices for parent lookup -rather than bringing in parent key default.

Here is code for create page:

cshtml

CS

For the create page my question is- how can I default that value of PositionID/PositionNbr to be what it would be for all the existing child records on the previous grid page? This should be a standard child record create scenario- where parent key is prepopulated

I am not finding a whole lot of good examples on a google search for how to prepopulate the parent foreign key in the child records -particularly when the creation is on a separate create page.

Hope my question is making sense- thanks in advance...

Modified- here is code as code

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
using ThePositionerRazor2.Models;

namespace ThePositionerRazor2.Pages.PositionDetail
{
    public class CreateModel : PageModel
    {
        private readonly ThePositionerRazor2.Models.WorkManagerV4Context _context;

        public CreateModel(ThePositionerRazor2.Models.WorkManagerV4Context context)
        {
            _context = context;
        }

        public IActionResult OnGet()
        {
        ViewData["ImportanceId"] = new SelectList(_context.Imp, "ImportanceId", "ImportanceId");
        ViewData["Knowdepth"] = new SelectList(_context.Knowdep, "DepthId", "DepthId");
        ViewData["PositionId"] = new SelectList(_context.Possummary, "PositionId", "PositionNbr");
        ViewData["TimeSpent"] = new SelectList(_context.Timescale, "TimevalId", "TimevalId");
        ViewData["Workitem"] = new SelectList(_context.Workhier, "Workitemid", "Workitemid");
            return Page();
        }

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

        // To protect from overposting attacks, enable the specific properties you want to bind to, for
        // more details, see https://aka.ms/RazorPagesCRUD.
        public async Task<IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return Page();
            }

            _context.Posdetail.Add(Posdetail);
            await _context.SaveChangesAsync();

            return RedirectToPage("./Index");
        }
    }
}

and

    @page
@model ThePositionerRazor2.Pages.PositionDetail.CreateModel

@{
    ViewData["Title"] = "Create";
}

<h1>Create</h1>

<h4>Posdetail</h4>
<hr />
<div class="row">
    <div class="col-md-4">
        <form method="post">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <div class="form-group">
                <label asp-for="Posdetail.PositionId" class="control-label"></label>
                <select asp-for="Posdetail.PositionId" class ="form-control" asp-items="ViewBag.PositionId"></select>
            </div>
            <div class="form-group">
                <label asp-for="Posdetail.Workitem" class="control-label"></label>
                <select asp-for="Posdetail.Workitem" class ="form-control" asp-items="ViewBag.Workitem"></select>
            </div>
            <div class="form-group">
                <label asp-for="Posdetail.TimeSpent" class="control-label"></label>
                <select asp-for="Posdetail.TimeSpent" class ="form-control" asp-items="ViewBag.TimeSpent"></select>
            </div>
            <div class="form-group">
                <label asp-for="Posdetail.ImportanceId" class="control-label"></label>
                <select asp-for="Posdetail.ImportanceId" class ="form-control" asp-items="ViewBag.ImportanceId"></select>
            </div>
            <div class="form-group">
                <label asp-for="Posdetail.TimeNrmz" class="control-label"></label>
                <input asp-for="Posdetail.TimeNrmz" class="form-control" />
                <span asp-validation-for="Posdetail.TimeNrmz" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Posdetail.Knowdepth" class="control-label"></label>
                <select asp-for="Posdetail.Knowdepth" class ="form-control" asp-items="ViewBag.Knowdepth"></select>
            </div>
            <div class="form-group">
                <label asp-for="Posdetail.Need" class="control-label"></label>
                <input asp-for="Posdetail.Need" class="form-control" />
                <span asp-validation-for="Posdetail.Need" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Posdetail.TimeCalc" class="control-label"></label>
                <input asp-for="Posdetail.TimeCalc" class="form-control" />
                <span asp-validation-for="Posdetail.TimeCalc" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Posdetail.Postskval" class="control-label"></label>
                <input asp-for="Posdetail.Postskval" class="form-control" />
                <span asp-validation-for="Posdetail.Postskval" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Posdetail.Ftetime" class="control-label"></label>
                <input asp-for="Posdetail.Ftetime" class="form-control" />
                <span asp-validation-for="Posdetail.Ftetime" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Posdetail.Ftesal" class="control-label"></label>
                <input asp-for="Posdetail.Ftesal" class="form-control" />
                <span asp-validation-for="Posdetail.Ftesal" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Posdetail.Lastupdated" class="control-label"></label>
                <input asp-for="Posdetail.Lastupdated" class="form-control" />
                <span asp-validation-for="Posdetail.Lastupdated" class="text-danger"></span>
            </div>
            <div class="form-group">
                <input type="submit" value="Create" class="btn btn-primary" />
            </div>
        </form>
    </div>
</div>

<div>
    <a asp-page="Index">Back to List</a>
</div>

@section Scripts {
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}

addtional info:

The relevant models

    using System;
using System.Collections.Generic;

namespace ThePositionerRazor2.Models
{
    public partial class Possummary
    {
        public Possummary()
        {
            Posdetail = new HashSet<Posdetail>();
        }

        public int PositionId { get; set; }
        public string PositionNbr { get; set; }
        public string WorkTitle { get; set; }
        public string Purpose { get; set; }
        public double? JobValue { get; set; }
        public double? TimeTotal { get; set; }
        public double? Fte { get; set; }
        public double? Salary { get; set; }
        public DateTime? Lastupdated { get; set; }
        public string JobFamily { get; set; }
        public int? DescriptionTypeId { get; set; }

        public virtual Descriptiontype DescriptionType { get; set; }
        public virtual ICollection<Posdetail> Posdetail { get; set; }
    }
}

and

    using System;
using System.Collections.Generic;

namespace ThePositionerRazor2.Models
{
    public partial class Posdetail
    {
        public int PosdetailId { get; set; }
        public int PositionId { get; set; }
        public int Workitem { get; set; }
        public int? TimeSpent { get; set; }
        public int? ImportanceId { get; set; }
        public double? TimeNrmz { get; set; }
        public int? Knowdepth { get; set; }
        public int? Need { get; set; }
        public int? TimeCalc { get; set; }
        public double? Postskval { get; set; }
        public double? Ftetime { get; set; }
        public double? Ftesal { get; set; }
        public DateTime Lastupdated { get; set; }

        public virtual Imp Importance { get; set; }
        public virtual Knowdep KnowdepthNavigation { get; set; }
        public virtual Possummary Position { get; set; }
        public virtual Timescale TimeSpentNavigation { get; set; }
        public virtual Workhier WorkitemNavigation { get; set; }
    }
}

Confirmation of your requirement:

In your main Index page,you have a List Possummary .When you click the Details link,it will display the details of list Posdetail which belongs to the Possummary you choose in Details page.Then you click the Create link, it will display the Possummary 's PositionId which you choose at the begining in Create page.

To meet your requirement:

You could pass the PositionId in the Create link.Then set the default selected value for selectlist by this PositionId :

public IActionResult OnGet(int PositionID)
{
    ViewData["ImportanceId"] = new SelectList(_context.Imp, "ImportanceId", "ImportanceId");
    ViewData["Knowdepth"] = new SelectList(_context.Knowdep, "DepthId", "DepthId");

    //change here....
    ViewData["PositionId"] = new SelectList(_context.Possummary, "PositionId", "PositionNbr", PositionID);
    ViewData["TimeSpent"] = new SelectList(_context.Timescale, "TimevalId", "TimevalId");
    ViewData["Workitem"] = new SelectList(_context.Workhier, "Workitemid", "Workitemid");
    return Page();
}

The whole working demo:

Model:

public class Possummary
{
    public Possummary()
    {
        Posdetail = new HashSet<Posdetail>();
    }
    [Key]
    public int PositionId { get; set; }
    public string PositionNbr { get; set; }
    public string WorkTitle { get; set; }
    public string Purpose { get; set; }
    public double? JobValue { get; set; }
    public double? TimeTotal { get; set; }
    public double? Fte { get; set; }
    public double? Salary { get; set; }
    public DateTime? Lastupdated { get; set; }
    public string JobFamily { get; set; }
    public int? DescriptionTypeId { get; set; }

    public virtual Descriptiontype DescriptionType { get; set; }
    public virtual ICollection<Posdetail> Posdetail { get; set; }
}
public class Descriptiontype
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public partial class Posdetail
{
    public int PosdetailId { get; set; }
    public int PositionId { get; set; }
    public int Workitem { get; set; }
    public int? TimeSpent { get; set; }
    public int? ImportanceId { get; set; }
    public double? TimeNrmz { get; set; }
    public int? Knowdepth { get; set; }
    public int? Need { get; set; }
    public int? TimeCalc { get; set; }
    public double? Postskval { get; set; }
    public double? Ftetime { get; set; }
    public double? Ftesal { get; set; }
    public DateTime Lastupdated { get; set; }

    public virtual Imp Importance { get; set; }
    public virtual Knowdep KnowdepthNavigation { get; set; }
    public virtual Possummary Position { get; set; }
    public virtual Timescale TimeSpentNavigation { get; set; }
    public virtual Workhier WorkitemNavigation { get; set; }
}
public class Imp
{
    [Key]
    public int ImportanceId { get; set; }
    public string Name { get; set; }
}
public class Knowdep
{
    [Key]
    public int DepthId { get; set; }
    public string Name { get; set; }
}
public class Timescale
{
    [Key]
    public int TimevalId { get; set; }
    public string Name { get; set; }
}
public class Workhier
{
    [Key]
    public int Workitemid { get; set; }
    public string Name { get; set; }
}

Possummarys/Index.cshtml :

@page
@model IndexModel
<h1>Index</h1>

<p>
    <a asp-page="Create">Create New</a>
</p>
<table class="table">
    <thead>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.Possummary[0].PositionNbr)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Possummary[0].WorkTitle)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Possummary[0].Purpose)
            </th>
     
            <th></th>
        </tr>
    </thead>
    <tbody>
@foreach (var item in Model.Possummary) {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.PositionNbr)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.WorkTitle)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Purpose)
            </td>          
            <td>
                <a asp-page="./Edit" asp-route-id="@item.PositionId">Edit</a> |
                <a asp-page="./Details" asp-route-id="@item.PositionId">Details</a> |
                <a asp-page="./Delete" asp-route-id="@item.PositionId">Delete</a>
            </td>
        </tr>
}
    </tbody>

Possummarys/Index.cshtml.cs:

namespace RazorProj3_1.Pages.Possummarys
{
    public class IndexModel : PageModel
    {
        private readonly RazorProj3_1Context _context;

        public IndexModel(RazorProj3_1Context context)
        {
            _context = context;
        }

        public IList<Possummary> Possummary { get;set; }

        public async Task OnGetAsync()
        {
            Possummary = await _context.Possummary.ToListAsync();
        }
    }
}

Possummarys/Details.cshtml:

@page
@model DetailsModel
     

    //focus here...
    <a asp-page="/PositionDetail/Create" asp-route-PositionID="@Model.Posdetail[0].PositionId" >Create New</a>
<h1>Postition Detail</h1>
<table class="table">
    <thead>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.Posdetail[0].Position)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Posdetail[0].WorkitemNavigation)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Posdetail[0].TimeNrmz)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Posdetail[0].Importance)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Posdetail[0].Lastupdated)
            </th>
            <th></th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model.Posdetail)
        {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Position.PositionNbr)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.WorkitemNavigation.Workitemid)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.TimeNrmz)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.ImportanceId)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Lastupdated)
            </td>
            <td>
                <a asp-page="./Edit" asp-route-id="@item.PosdetailId">Edit</a> |
                <a asp-page="./Details" asp-route-id="@item.PosdetailId">Details</a> |
                <a asp-page="./Delete" asp-route-id="@item.PosdetailId">Delete</a>
            </td>
        </tr>
        }
    </tbody>
</table>

Possummarys/Details.cshtml.cs:

public class DetailsModel : PageModel
{
    private readonly RazorProj3_1Context _context;

    public DetailsModel(.RazorProj3_1Context context)
    {
        _context = context;
    }

    public IList<Posdetail> Posdetail { get; set; }

    public async Task<IActionResult> OnGetAsync(int? id)
    {

        Posdetail = await _context.Posdetail.Include(p=>p.Position)
                            .Include(p=>p.WorkitemNavigation)
                            .Where(a=>a.PositionId==id).ToListAsync();

        return Page();
    }
}

PositionDetail/Create.cshtml: (the same as what you provided)

@page
@model CreateModel

@{
    ViewData["Title"] = "Create";
}
<h1>Create</h1>

<h4>Posdetail</h4>
<hr />
<div class="row">
    <div class="col-md-4">
        <form method="post">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <div class="form-group">
                <label asp-for="Posdetail.PositionId" class="control-label"></label>
                <select asp-for="Posdetail.PositionId" class="form-control" asp-items="ViewBag.PositionId"></select>
            </div>
            <div class="form-group">
                <label asp-for="Posdetail.Workitem" class="control-label"></label>
                <select asp-for="Posdetail.Workitem" class="form-control" asp-items="ViewBag.Workitem"></select>
            </div>
            <div class="form-group">
                <label asp-for="Posdetail.TimeSpent" class="control-label"></label>
                <select asp-for="Posdetail.TimeSpent" class="form-control" asp-items="ViewBag.TimeSpent"></select>
            </div>
            <div class="form-group">
                <label asp-for="Posdetail.ImportanceId" class="control-label"></label>
                <select asp-for="Posdetail.ImportanceId" class="form-control" asp-items="ViewBag.ImportanceId"></select>
            </div>
            <div class="form-group">
                <label asp-for="Posdetail.TimeNrmz" class="control-label"></label>
                <input asp-for="Posdetail.TimeNrmz" class="form-control" />
                <span asp-validation-for="Posdetail.TimeNrmz" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Posdetail.Knowdepth" class="control-label"></label>
                <select asp-for="Posdetail.Knowdepth" class="form-control" asp-items="ViewBag.Knowdepth"></select>
            </div>
            <div class="form-group">
                <label asp-for="Posdetail.Need" class="control-label"></label>
                <input asp-for="Posdetail.Need" class="form-control" />
                <span asp-validation-for="Posdetail.Need" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Posdetail.TimeCalc" class="control-label"></label>
                <input asp-for="Posdetail.TimeCalc" class="form-control" />
                <span asp-validation-for="Posdetail.TimeCalc" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Posdetail.Postskval" class="control-label"></label>
                <input asp-for="Posdetail.Postskval" class="form-control" />
                <span asp-validation-for="Posdetail.Postskval" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Posdetail.Ftetime" class="control-label"></label>
                <input asp-for="Posdetail.Ftetime" class="form-control" />
                <span asp-validation-for="Posdetail.Ftetime" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Posdetail.Ftesal" class="control-label"></label>
                <input asp-for="Posdetail.Ftesal" class="form-control" />
                <span asp-validation-for="Posdetail.Ftesal" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Posdetail.Lastupdated" class="control-label"></label>
                <input asp-for="Posdetail.Lastupdated" class="form-control" />
                <span asp-validation-for="Posdetail.Lastupdated" class="text-danger"></span>
            </div>
            <div class="form-group">
                <input type="submit" value="Create" class="btn btn-primary" />
            </div>
        </form>
    </div>
</div>

<div>
    <a asp-page="Index">Back to List</a>
</div>

@section Scripts {
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}

PositionDetail/Create.cshtml.cs:

public class CreateModel : PageModel
{
    private readonly RazorProj3_1Context _context;

    public CreateModel(RazorProj3_1Context context)
    {
        _context = context;
    }

    public IActionResult OnGet(int PositionID)
    {
        ViewData["ImportanceId"] = new SelectList(_context.Imp, "ImportanceId", "ImportanceId");
        ViewData["Knowdepth"] = new SelectList(_context.Knowdep, "DepthId", "DepthId");

        //change here....
        ViewData["PositionId"] = new SelectList(_context.Possummary, "PositionId", "PositionNbr", PositionID);
        ViewData["TimeSpent"] = new SelectList(_context.Timescale, "TimevalId", "TimevalId");
        ViewData["Workitem"] = new SelectList(_context.Workhier, "Workitemid", "Workitemid");
        return Page();
    }

    [BindProperty]
    public Posdetail Posdetail { get; set; }
   
    public async Task<IActionResult> OnPostAsync()
    {
        //the same as yours...
    }
}

Result:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM