将动态控件添加到MVC并在Controller中获取它们的值 [英] Adding dynamic controls to MVC and getting their values in Controller

查看:234
本文介绍了将动态控件添加到MVC并在Controller中获取它们的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉,设置时间太长,但是您可以看到实际的问题来解决问题.

Sorry for making it too long but you can see actual problem to get issue.

背景: 它是一个e-commerce项目,在该项目中,我允许客户端在其下添加任意数量的类别和子类别.客户将为每个类别自己创建一些属性.假设客户端创建了一个类别Mobile Phones并将此类别添加到Category表中,然后他为该类别创建了一些属性,例如PriceBrand等,并且所有这些属性都通过Attributes表中>(Categories表的主键.).到目前为止已经完成了.

Background: Its a e-commerce project where I am allowing client to add any number of categories and subcategories under that. For each category client will create some attributes by himself. Suppose client created a category Mobile Phones and this category is added to Category table and then he creates some attributes to that category like Price,Brand etc and all these attributes are inserted into Attributes table with CategoryId (Primary Key of Categories table.). Its done so far.

当客户添加特定类别的产品时,例如Mobile Phones类别中的示例,并且为了保存产品,我具有Products表.产品表最初没有PriceBrand列,但是它们将在客户端创建属性时创建,因此在开发时我不知道所有这些列.

While client adds a products in particular category, as an example in our Mobile Phones's Category and for saving products I have Products Table. Product table do not have columns Price and Brand initially but they will be created when client creates an attribute hence I don't know all these columns at development time.

在创建属性客户端时,还将填充有关该属性的一些信息,即

While creating an attribute client will also fill some information regarding that attribute i.e

  1. 属性名称
  2. 显示名称
  3. 服务器控件,例如TextBoxDropdown
  4. 验证
  1. Name of attribute
  2. Display Name
  3. Server Control like TextBox,Dropdown etc
  4. Validations

实际问题:

通过上面的故事,我们知道对于任何类别我们可能具有任意数量的属性,并且这些属性将与Server Control关联.在添加产品时在视图上添加这些控件时,我们只需要动态添加关联的控件,而不是HTML或Razor.我们可以在MVC中做到这一点吗?如果是,那怎么办?

Looking at above story, we came to know that we may have any number of attribute for any category and those attributes will have Server Control associated. While adding those controls on view while adding a product, we need to add only associated control dynamically, not HTML or Razor. Can we do this in MVC.? If yes then how?

谢谢.

推荐答案

您可以在控制器的操作中使用Dictionary或数组作为参数.在视图中,您只需要命名所有输入,就好像它们是一个数组一样:

You can use a Dictionary or an array as a parameter in your controller's actions. In the view you'll only need to name all your inputs as if they were an array:

<input type="text" name="data[1]" >
<input type="text" name="data[2]" >

以下是示例: http://www.quickstepit.net/mvc -3-model-binding-array/

选择同样适用.

这篇关于将动态控件添加到MVC并在Controller中获取它们的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆