如何向表中动态添加行并回发数据以查看

时间:2018-09-19 15:21:32

标签: asp.net entity-framework asp.net-mvc-4

该问题表明我在ASP.NET应用程序中使用ENTITY FRAMEWORK,其中有一个表,用户可以向其中添加行并向其中输入数据。我需要知道如何将这些数据发布回控制器。

我的模型:(当我使用数据库优先方法时,是由EF生成的)

public partial class TRA_FORMS
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public TRA_FORMS()
        {
            this.TRA_ITEMS = new HashSet<TRA_ITEMS>();

        }

        public int ID { get; set; }
        [Display(Name = "Name of TRA")]
        public string NAME_OF_TRA { get; set; }
        [Display(Name = "Requested amount")]
        public Nullable<double> AMOUNT_REQUESTED { get; set; }
        [Display(Name = "Name of commitee member making application")]
        public string COMMITEE_MEMBER_NAME { get; set; }
        [Display(Name = "Date of last AGM")]
        public Nullable<System.DateTime> DATE_OF_AGM { get; set; }
        [Display(Name = "Signed")]
        public string SIGNED { get; set; }
        [Display(Name = "Dated")]
        public Nullable<System.DateTime> DATED { get; set; }



        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<TRA_ITEMS> TRA_ITEMS { get; set; }
    }


public partial class TRA_ITEMS
    {

        public int ITEM_ID { get; set; }
        [Display(Name = "Description of Items")]
        public string DESC_OF_ITEMS { get; set; }
        [Display(Name = "Quantity")]
        public Nullable<int> QUANTITY { get; set; }
        [Display(Name = "Cost")]
        public Nullable<double> COST { get; set; }
        [Display(Name = "From ID")]
        public Nullable<int> FORM_ID { get; set; }

        public virtual TRA_FORMS TRA_FORMS { get; set; }
    }

我的控制器:

[HttpPost]

        public ActionResult Index(TRA_FORMS traForms)
        {

            return View();
        }

我在这里没有显示我的观点,因为这是错误的,我不知道该怎么做。但是视图接受类型为TRA_FORMS的模型。我在此视图中有一个表(常规html),该表具有3列-每列用于TRA_ITEMS模型的属性。项目,数量和成本的描述。用户可以向该表添加任意数量的项目。理想情况下,在回发时应该回发一个列表,其中包含用户添加的每个项目,但我不知道该怎么做。我已经研究了与此相关的几个Stack溢出问题,并且整天都在寻找解决方案,但是我是ASP.NET的新手,因此无法将我发现的大多数答案应用于我的方案。

0 个答案:

没有答案