如何在asp.net mvc 4模型对话框中发布用户动态添加的数据

我必须使用ASP.NET MVC 4中的模型对话框实现创建/编辑function,使用Entity Framework数据库第一种方法,如下图所示:

在此处输入图像描述

我在网上搜索解决方案的时候已经阅读了以下文章。

点击这里浏览

它与我想要的非常相似,但唯一的区别是在我的情况下,Emp名称和Emp代码是静态的,但是用户可以在Field 1Field 2Comments部分的客户端动态添加行,所以在这种情况下如何保存(创建/编辑)数据(由用户动态添加)到数据库中。

tblEmployee – >此表包含EmpID,Emp名称和Emp代码列

tblTable1 – >此表有ID(主键),EmpID(外键),Detail1

tblTable2 – >此表有ID(主键),EmpID(外键),Detail2

我是否必须使用json / ajax方法或您建议的其他方法。

谢谢

在控制器的参数中,将动态属性设置为数组。

在客户端添加新行时,将控件命名为与参数名称相同的名称。 即:

   

当您发布到此控制器时,您应该看到所有动态数据已填满您的arrays。 你可以在它到达时处理密钥。

如果用户可以动态添加Detail1Detail2Comment ,将它们定义为模型,然后添加到以下操作:

 public class Detail1 { public string Content {get; set;} } public class Detail2 { public string Content {get; set;} } public class Comment { public string Content {get; set;} } [HttpPost] public ActionResult Create(string EmpName, string EmpCode, List detail1s, List detail2s, List comments) { //save operations... } 

并查看:

 @using (Html.BeginForm()) { // other static fields..    
Detail 1
Detail 2
Comments
}

在员工保存后,您可以将EmpId值赋予3个模型。 首先保存员工,然后你会得到它的Id ,然后给Detail1Detail2Comment模型提供taht值并再次保存。

希望,这会奏效。