简体   繁体   English

在Asp.net MVC剃刀视图中的动态表中添加和删除行

[英]Adding and deleting rows in dynamic table in Asp.net mvc razor view

I am new to asp.net mvc. 我是asp.net mvc的新手。 In my project i have to add a table where user can enter details and select options from drop down . 在我的项目中,我必须添加一个表,用户可以在其中输入详细信息并从下拉菜单中选择选项。 And when the user clicks add button a row must be added to the table with all fields(that includes textbox, dropdown list). 当用户单击添加按钮时,必须将具有所有字段(包括文本框,下拉列表)的行添加到表中。 Also when the user clicks on delete button that particular row must be removed from the table. 同样,当用户单击删除按钮时,必须从表中删除该特定行。 By default when the page loads I should have two rows in the table with all required fields. 默认情况下,当页面加载时,表中应该有两行包含所有必填字段。 I need solution for the following questions 我需要以下问题的解决方案

  1. How to achieve the above said task. 如何完成上述任务。
  2. I also need to save the table details. 我还需要保存表格详细信息。 So how to pass the table details to the controller ? 那么如何将表的详细信息传递给控制器​​呢?
  3. After saving the details when the user clicks on edit button I should load all the details in the table. 在用户单击“编辑”按钮时保存了详细信息之后,我应该将所有详细信息加载到表中。 Please help me in this 请帮我

I also searched a lot but none of the example includes dropdown option in dynamic table. 我也搜索了很多,但是没有一个示例在动态表中包含下拉选项。 Please help me in this task. 请帮助我完成此任务。 Thanks in advance for all the volunteers. 在此先感谢所有志愿者。

I suggest using the BeginCollectionItem() helper that Steve Sanderson created 我建议使用Steve Sanderson创建的BeginCollectionItem()帮助器

http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/ http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/

it achieves exactly what you want, and he goes over it step by step with a working example. 它完全可以实现您想要的功能,他通过一个有效的示例逐步介绍了它。 I've used it in several of my projects. 我已经在几个项目中使用过它。

I would have left some links in a comment, but I don't have enough rep yet. 我会在评论中留下一些链接,但是我没有足够的代表。 So I'll put this here as an answer and expound on it more than I would in a comment. 因此,我将其放在此处作为答案,并比我在评论中进行更多的阐述。 But this is just general info. 但这只是一般信息。 Please provide more details for more specific answers. 请提供更多详细信息以获得更具体的答案。

First of all, what version of MVC are you working with? 首先,您正在使用哪个版本的MVC? What does your data model look like? 您的数据模型是什么样的? Are you using an EntityModel? 您正在使用EntityModel吗? Have you considered a client side MVC or MVVM framework or library, such as AngularJS or Knockout? 您是否考虑过客户端MVC或MVVM框架或库,例如AngularJS或Knockout?

Contingent on your answers to those questions, an answer to your question might be... 视您对这些问题的答案而定,对您问题的答案可能是...

  1. Create a class for your data model, and start decorating it with data annotations . 为您的数据模型创建一个类,然后开始使用数据注释对其进行修饰。 Each column that you want to be appear in the table should be exposed as a public property. 您希望在表中显示的每一列都应作为公共属性公开。 This can drive a code first DB approach . 这可以驱动代码优先的DB方法 Alternatively you could build an EntityModel from an existing DB. 或者,您可以从现有数据库构建EntityModel。 If you're going to do that, you might want to look in to modifying the T4 templates used by the EF wizard , to add data annotations to the entity models generated. 如果要执行此操作,则可能需要研究修改EF向导使用T4模板 ,以将数据注释添加到生成的实体模型中。
  2. You can use out of the box scaffolding to generate razor templates for CRUD operations . 您可以使用开箱即用的脚手架为CRUD操作生成剃刀模板 You could then use your new T4 skills to modify the scaffolding templates . 然后,您可以使用新的T4技能来修改脚手架模板
  3. Consider using WebAPI and/or SignalR with a client side framework to either provide a RESTful api or persistent connection, either of which can be used to sync application level data between client and server. 考虑将WebAPI和/或SignalR与客户端框架一起使用,以提供RESTful api或持久连接,这两种方法均可用于在客户端和服务器之间同步应用程序级别的数据。

It might seem like a good bit of overhead, but 1 & 2 can save you a lot of time if you're going to maximize use of Razor views. 这看起来似乎有点开销,但是如果您要最大限度地使用Razor视图,则1和2可以节省大量时间。

You may want to only exchange application level data with the client most of the time though. 但是,您可能大部分时间只希望与客户端交换应用程序级别的数据。 You could build a client side architecture using a framework like AngularJS , or build your own framework around a library like Knockout , to bind to your data, and allow the markup (such as a custom directive's template in AngularJS, for example) to actually be generated and rendered via a client side controller. 您可以使用诸如AngularJS之类的框架构建客户端架构,或者围绕诸如Knockout之类的库构建自己的框架,以绑定到数据,并允许标记(例如,例如AngularJS中的自定义指令模板)通过客户端控制器生成和呈现。

For an opensource, standardized look and feel, I would recommend looking into Boostrap for a set relatively easy to use of components and layout/styling conventions. 对于开放源代码 ,标准化的外观和感觉,我建议对Boostrap进行研究,以获取一组相对易于使用的组件和布局/样式约定。 You can use it barebones or incorporate it with other js libs, like this . 您可以准系统使用它,也可以将其与其他js库合并, 例如this

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM