Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net C#MVC需要一个模型/视图模型,它可以提供相同的视图(相同的表)并链接到两个不同的字段_C#_Asp.net Mvc - Fatal编程技术网

Asp.net C#MVC需要一个模型/视图模型,它可以提供相同的视图(相同的表)并链接到两个不同的字段

Asp.net C#MVC需要一个模型/视图模型,它可以提供相同的视图(相同的表)并链接到两个不同的字段,c#,asp.net-mvc,C#,Asp.net Mvc,我已经找到了所有可能的类似问题的建议以及其他谷歌搜索。但一切都不起作用。 可能是因为我是MVC的新手 我的问题是,我有一个发票主数据,它作为两个字段,分别为账单到发票ID和提供发票到发票ID。 两者都是从一个账单到另一个账单 现在在Invoice_Master视图中,我希望合并相同的内容。但尝试了这么多的搜索选项,但都没有得到满足。取而代之的是两个不同的字段,如Billing\u To\u ID,来自两个不同模型的Project\u ID工作正常 到目前为止,我已经完成了以下代码:- 模型计费\

我已经找到了所有可能的类似问题的建议以及其他谷歌搜索。但一切都不起作用。 可能是因为我是MVC的新手

我的问题是,我有一个发票主数据,它作为两个字段,分别为账单到发票ID和提供发票到发票ID。 两者都是从一个账单到另一个账单

现在在Invoice_Master视图中,我希望合并相同的内容。但尝试了这么多的搜索选项,但都没有得到满足。取而代之的是两个不同的字段,如Billing\u To\u ID,来自两个不同模型的Project\u ID工作正常

到目前为止,我已经完成了以下代码:- 模型计费\u到\u主机

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; }
    }
}
发票范本

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>
@model IEnumerable
@{
Layout=“~/Views/Shared/_Layout.cshtml”;
}

@ActionLink(“新建”、“创建”)

@Html.DisplayNameFor(model=>model.Invoice\u Type\u Master\u Obj.Invoice\u Type\u Name) @Html.DisplayNameFor(model=>model.Invoice\u No) @Html.DisplayNameFor(模型=>model.Invoice\u日期) @DisplayNameFor(model=>model.Billing\u To\u Master\u Obj.Billing\u To\u Name) @DisplayNameFor(model=>model.suppling_To_Master_Obj.Billing_To_Name) @DisplayNameFor(model=>model.Vendor\u代码) @Html.DisplayNameFor(model=>model.Fieldwork\u Center) @Html.DisplayNameFor(model=>model.Fieldwork\u日期) @Html.DisplayNameFor(model=>model.Kind\u Attn) @Html.DisplayNameFor(model=>model.Invoice\u主题) @DisplayNameFor(model=>model.Project\u Master\u obj.Project\u Name) @DisplayNameFor(model=>model.Category\u Master\u obj.Category\u Name) @DisplayNameFor(model=>model.Brand\u Master\u obj.Brand\u Name) @DisplayNameFor(model=>model.Town) @DisplayNameFor(model=>model.Tax\u代码) @DisplayNameFor(model=>model.Reference) @foreach(模型中的var项目) { @Html.DisplayFor(modelItem=>item.Invoice\u Type\u Master\u Obj.Invoice\u Type\u Name) @Html.DisplayFor(modelItem=>item.Invoice\u No) @Html.DisplayFor(modelItem=>item.Invoice\u日期) @DisplayFor(modelItem=>item.Billing\u To\u Master\u Obj.Billing\u To\u Name) @DisplayFor(modelItem=>item.Suppling\u To\u Master\u Obj.Billing\u To\u Name) @DisplayFor(modelItem=>item.Vendor\u代码) @DisplayFor(modelItem=>item.Fieldwork\u Center) @DisplayFor(modelItem=>item.Fieldwork\u日期) @DisplayFor(modeleItem=>item.Kind\u Attn) @Html.DisplayFor(modelItem=>item.Invoice\u主题) @DisplayFor(modelItem=>item.Project\u Master\u obj.Project\u Name) @DisplayFor(modelItem=>item.Category\u Master\u obj.Category\u Name) @DisplayFor(modelItem=>item.Brand\u Master\u obj.Brand\u Name) @DisplayFor(modeleItem=>item.Town) @DisplayFor(modelItem=>item.Tax\u代码) @DisplayFor(modelItem=>item.Reference) @ActionLink(“编辑”,“编辑”,新的{id=item.Invoice\u id}) @ActionLink(“删除”,“删除”,新的{id=item.Invoice\u id}) }

任何相同的解决方案都是非常可观的。

我认为,您应该在发票主控中使用Billing To Id作为外键

如果在这种情况下,由于业务限制而无法使用外键,则可以使用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; }
}
公共类BillingViewModel
{
//如果需要,可以使用列表
公共计费器到主计费器{get;set;}
公共发票\u主发票主发票{get;set;}
}

根据贾耶什·坦纳的回答,我详细介绍了外键。我先删除了所有关系。并再次分配外键。 帮助我进一步追查答案

使用[ForeignKey(]后,问题得到解决

    [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; }

你关于外键的建议对我帮助很大。请参考我发布的答案。如果你编辑了你的answe,并且可以有一些相同的更详细的解决方案,我很乐意接受你的答案。@YeshwantMudholkar是的,这是一条路。如果没有业务约束和sql,那么你肯定应该建立关系。除此之外,我还需要我们建议重构您的属性名称。请参见此示例:类产品不需要“ProductId”作为其属性,而应仅为“Id”。考虑代码结构的可读性和一致性。因此,什么是易于阅读的:Product.ProductId或Product.Id