How to display data of foreign key already when open create.cshtml page in browser? so that we can insert only those values which are to be inserted others from foreign key gets fetched and showed?
Fetch data of foreign key to make form better and easy how to do it?
Purchase.cs in models folder
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace ShoppingAPI.Models
{
public class Purchase
{
[Key]
public int Purchase_Id { get; set; }
public int User_Id { get; set; }
[ForeignKey("User_Id")]
public Users Users { get; set; }
// public string Username { get; set; }
public int Total_Items { get; set; }
public int Active { get; set; }
public int Item_Id { get; set; }
[ForeignKey("Item_Id")]
public Inventory Inventory { get; set; }
public DateTime Created_Date { get; set; }
public string Created_By { get; set; }
}
}
in Views folder contains Purchase folder that contains
Create.cshtml file
@model ShoppingAPI.Models.Purchase
@{
ViewData["Title"] = "Create";
}
<h1>Create</h1>
<h4>Products</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="Create">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<!--<div class="form-group">
<label asp-for="Purchase_Id" class="control-label"></label>
<input asp-for="Purchase_Id" class="form-control" />-->
@*<span asp-validation-for="Purchase_Id" class="text-danger"></span>*@
<!--</div>-->
<div class="form-group">
<label asp-for="User_Id" class="control-label"></label>
<input asp-for="User_Id" class="form-control" />
<span asp-validation-for="User_Id" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Item_Id" class="control-label"></label>
<input asp-for="Item_Id" class="form-control" />
<span asp-validation-for="Item_Id" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Total_Items" class="control-label"></label>
<input asp-for="Total_Items" class="form-control" />
<span asp-validation-for="Total_Items" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Active" class="control-label"></label>
<input asp-for="Active" class="form-control" />
<span asp-validation-for="Active" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Created_By" class="control-label"></label>
<input asp-for="Created_By" class="form-control" />
<span asp-validation-for="Created_By" 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-action="Index">Back to List</a>
</div>
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
How to display data of foreign key already when open create.cshtml page in browser? so that we can insert only those values which are to be inserted others from foreign key gets fetched and showed?
Fetch data of foreign key to make form better and easy how to do it?
question from:
https://stackoverflow.com/questions/65908149/show-data-of-foreign-key-column-in-create-cshtml-in-asp-net-core-web-api 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…