Asp. net C# MVC Требуется модель / модель представления, которая может обеспечить одинаковое представление (одну и ту же таблицу) и ссылку на два разных поля. - PullRequest
0 голосов
/ 27 апреля 2020

Я уже обращался ко всем и выкладывал возможные рекомендации по подобным вопросам, а также по другим поискам в Google. Но ничего не работает. Может быть, так как я новичок в MVC.

Моя проблема в том, что у меня есть Invoice_Master, который представляет собой два поля Billing_To_ID и Supplying_To_ID. Оба исходят из одного Billing_To_Master.

Теперь в Invoice_Master View я хочу включить одно и то же. Но попробовал так много вариантов из поиска, но ни один не выполняется. Вместо этого два разных поля, таких как Billing_To_ID, Project_ID из двух разных моделей работают нормально.

У меня есть следующие коды, сделанные до сих пор: - модель Billing_To_Master

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace ACBooks.Models
{
    public class Billing_To_Master
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        [Display(Name = "Billing To ID")]
        public decimal Billing_To_ID { get; set; }
        [Display(Name = "Billing To")]
        public string Billing_To_Name { get; set; }

        [Display(Name = "Address")]
        public string Address { get; set; }

        [Display(Name = "State")]
        public string State { get; set; }

        [Display(Name = "Pincode")]
        public string Pincode { get; set; }

        [Display(Name = "ARN")]
        public string ARN { get; set; }

        [Display(Name = "GST")]
        public string GST { get; set; }

        [Display(Name = "PAN")]
        public string PAN { get; set; }

        [Display(Name = "SAC")]
        public string SAC { get; set; }

        [Display(Name = "Contact Name")]
        public string Contact_Name { get; set; }

        [Display(Name = "Mobile")]
        public string Mobile { get; set; }

        [Display(Name = "Landline")]
        public string Landline { get; set; }

        [Display(Name = "Flag ID")]
        public bool Billing_To_Flag_ID { get; set; }
    }
}

модель Invoice_Master

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace ACBooks.Models
{
    public class Invoice_Master
    {


        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        [Display(Name = "Invoice_ID")]
        public decimal Invoice_ID { get; set; }

        [Display(Name = "Invoice Type")]
        public decimal Invoice_Type_ID { get; set; }

        [Display(Name = "Invoice Type Name")]
        public virtual Invoice_Type_Master Invoice_Type_Master_Obj { get; set; }

        [Display(Name = "Invoice No")]
        public string Invoice_No { get; set; }

        [Display(Name = "Invoice Date")]
        [DataType(DataType.Date), DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
        public DateTime Invoice_Date { get; set; }

        [Display(Name = "Billing To")]
        public decimal Billing_To_ID { get; set; }

        [Display(Name = "Billing To Name")]
        public virtual Billing_To_Master Billing_To_Master_Obj { get; set; }

        [Display(Name = "Supplying To")]
        public decimal Supplying_To_ID { get; set; }

        //Trying this - but not working
        [Display(Name = "Supplying To")]
        public virtual Billing_To_Master Supplying_To_Master_Obj { get; set; }

        [Display(Name = "Client Code")]
        public string Vendor_Code { get; set; }

        [Display(Name = "Field Work Center")]
        public string Fieldwork_Center { get; set; }

        [Display(Name = "Field Work Dates")]
        public string Fieldwork_Dates { get; set; }

        [Display(Name = "Kind Attention")]
        public string Kind_Attn { get; set; }

        [Display(Name = "Invoice Subject")]
        public string Invoice_Subject { get; set; }

        [Display(Name = "Project ID")]
        public decimal Project_ID { get; set; }

        [Display(Name = "Project Name")]
        public virtual Project_Master Project_Master_obj { get; set; }

        [Display(Name = "Category ID")]
        public decimal Category_ID { get; set; }

        [Display(Name = "Category Name")]
        public virtual Category_Master Category_Master_obj { get; set; }

        [Display(Name = "Brand ID")]
        public decimal Brand_ID { get; set; }

        [Display(Name = "Brand Name")]
        public virtual Brand_Master Brand_Master_obj { get; set; }

        [Display(Name = "Town")]
        public string Town { get; set; }

        [Display(Name = "Tax Code")]
        public string Tax_Code { get; set; }

        [Display(Name = "Reference")]
        public string Reference { get; set; }

    }
}

и это мое мнение

@model IEnumerable<ACBooks.Models.Invoice_Master>

    @{
        Layout = "~/Views/Shared/_Layout.cshtml";
    }

    <p>
        @Html.ActionLink("Create New", "Create")
    </p>
    <table class="table">
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.Invoice_Type_Master_Obj.Invoice_Type_Name)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Invoice_No)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Invoice_Date)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Billing_To_Master_Obj.Billing_To_Name)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Supplying_To_Master_Obj.Billing_To_Name)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Vendor_Code)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Fieldwork_Center)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Fieldwork_Dates)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Kind_Attn)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Invoice_Subject)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Project_Master_obj.Project_Name)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Category_Master_obj.Category_Name)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Brand_Master_obj.Brand_Name)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Town)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Tax_Code)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Reference)
            </th>
            <th></th>
        </tr>

        @foreach (var item in Model)
        {
            <tr>
                <th>
                    @Html.DisplayFor(modelItem => item.Invoice_Type_Master_Obj.Invoice_Type_Name)
                </th>
                <th>
                    @Html.DisplayFor(modelItem => item.Invoice_No)
                </th>
                <th>
                    @Html.DisplayFor(modelItem => item.Invoice_Date)
                </th>
                <th>
                    @Html.DisplayFor(modelItem => item.Billing_To_Master_Obj.Billing_To_Name)
                </th>
                <th>
                    @Html.DisplayFor(modelItem => item.Supplying_To_Master_Obj.Billing_To_Name)
                </th>
                <th>
                    @Html.DisplayFor(modelItem => item.Vendor_Code)
                </th>
                <th>
                    @Html.DisplayFor(modelItem => item.Fieldwork_Center)
                </th>
                <th>
                    @Html.DisplayFor(modelItem => item.Fieldwork_Dates)
                </th>
                <th>
                    @Html.DisplayFor(modelItem => item.Kind_Attn)
                </th>
                <th>
                    @Html.DisplayFor(modelItem => item.Invoice_Subject)
                </th>
                <th>
                    @Html.DisplayFor(modelItem => item.Project_Master_obj.Project_Name)
                </th>
                <th>
                    @Html.DisplayFor(modelItem => item.Category_Master_obj.Category_Name)
                </th>
                <th>
                    @Html.DisplayFor(modelItem => item.Brand_Master_obj.Brand_Name)
                </th>
                <th>
                    @Html.DisplayFor(modelItem => item.Town)
                </th>
                <th>
                    @Html.DisplayFor(modelItem => item.Tax_Code)
                </th>
                <th>
                    @Html.DisplayFor(modelItem => item.Reference)
                </th>
                <td>
                    @Html.ActionLink("Edit", "Edit", new { id = item.Invoice_ID })
                    @Html.ActionLink("Delete", "Delete", new { id = item.Invoice_ID })
                </td>
            </tr>
        }
    </table>

Любое разрешение для этого было бы весьма заметно.

Ответы [ 2 ]

1 голос
/ 27 апреля 2020

Я думаю, вы должны использовать Billing_To_Id в качестве внешнего ключа в invoice_master.

Если в этом случае вы не можете использовать внешний ключ из-за бизнес-ограничений, вы можете использовать ViewModel и отправить эту ViewModel для просмотра, как показано ниже .

public class BillingViewModel
{
    //You can use List<T> if you need to
    public Billing_To_Master BillingToMaster { get; set; }

    public Invoice_Master InvoiceMaster { get; set; }
}
0 голосов
/ 27 апреля 2020

Согласно Джей sh Ответ Танны, я подробно остановился на внешнем ключе. Я удалил все отношения в первую очередь. И снова назначил внешние ключи. Этот ответ помог мне дополнительно отследить ответ.

После использования [ForeignKey (<<em> ID >] и проблема была решена.

    [Display(Name = "Billing Name")]
    [ForeignKey("Billing_To_ID")]
    public virtual Billing_To_Master Billing_To_Master_Obj { get; set; }

    [Display(Name = "Supplying ID")]
    public decimal Supplying_To_ID { get; set; }

    [Display(Name = "Supplying Name")]

    [ForeignKey("Supplying_To_ID")]
    public virtual Billing_To_Master Supplying_To_Master_Obj { get; set; }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...