Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

Show data of foreign key column in create.cshtml in asp.net core web api?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...