[英]In .net core razor pages- how do you navigate from parent record page to a child record page?
[英]in dot net core razor pages - how do you default a dropdown choice on a create page?
我目前有正確導航到不同子頁面的父頁面。 子頁面具有子記錄的站立網格-並且它還具有“創建”鏈接以添加新的子記錄。
在標准實體框架腳手架下,“創建”頁面具有鏈接到“查找”值的字段的標准 html“下拉”列表。
下面是一些屏幕截圖:
Create New Link 會打開標准的腳手架創建頁面:
PositionId 字段顯示父查找選項的完整列表 - 而不是引入父鍵默認值。
這是創建頁面的代碼:
對於創建頁面,我的問題是 - 我如何將 PositionID/PositionNbr 的值默認為上一個網格頁面上所有現有子記錄的值? 這應該是標准的子記錄創建方案 - 預填充父鍵
我在谷歌搜索中沒有找到很多關於如何在子記錄中預填充父外鍵的好例子——特別是當創建是在單獨的創建頁面上時。
希望我的問題是有道理的-提前謝謝...
修改-這里是代碼即代碼
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");
}
}
}
和
@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");}
}
附加信息:
相關型號
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; }
}
}
和
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; }
}
}
確認您的要求:
在您的主索引頁面中,您有一個 List Possummary
。當您單擊Details
鏈接時,它將顯示屬於您在Details
頁面中選擇的Possummary的 List Posdetail
的詳細信息。然后單擊Create
鏈接,它將顯示Possummary您在Create
頁面開始時選擇的 PositionId。
為了滿足您的要求:
您可以在Create
鏈接中傳遞PositionId
。然后通過此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();
}
整個工作演示:
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:(和你提供的一樣)
@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...
}
}
結果:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.