提交的JavaScript动态地添加元素像控制器#1的方法 [英] Submit javascript dynamically added elements to controller method like Stackoverflow

查看:145
本文介绍了提交的JavaScript动态地添加元素像控制器#1的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简单来说:我想,让用户选择在JS一些标签,并提交给我的控制器。这里是我的建议:

Put it simply: I'd like to let the user to select some tags in JS and submit it to my controller. Here are my suggestions:


  • 创建为每个插入标记一个隐藏的输入与命名约定,如: Tag123 (123 =这个标签的唯一标识符),并通过的FormCollection 在我的操作方法,以找出哪些标签已被选定。缺点是显而易见的:使用的FormCollection 而不是视图模型并通过的FormCollection 来得到我想要的数据似乎对我不好。

  • 创建一个隐藏的输入和每个选定的标签附加到它。这可以成为标签缺失凌乱,因为我觉得应该从输入的电流值正确的ID,并删除它。但是,Pro是我只有一个元素,可以把它在一个视图模型来访问它的控制器动作。

  • 好奇,想看看是否有人知道为什么会这样做的。他们现在有种defning标准。很想知道他们是如何做到这一点。

  • Create a hidden input for every inserted tag with naming convention like: Tag123 (123 = this tag's unique identifier) and iterate through FormCollection in my action method to find out which tags have been selected. Cons are obvious: using FormCollection instead of ViewModel and iterating through the FormCollection to get my desired data seems bad to me.
  • Create one hidden input and append every selected tag to it. This can become messy on tags deletion since I should find the right id from the input's current value and delete it. But the Pro is that I only have one element and can put it in a viewmodel to access it in controller action.
  • Curious to see if anyone knows how SO does it. They're kind of defning the standards now. Would love to know how they do it.

感谢。

推荐答案

我有一个网站与添加标签的选项运行,很像SO。结果
我对这个问题的方法,但是,导致我建立一个输入字段为每个添加标签,并增加一个javascript的首页的每次添加一个新的输入时间变量,然后利用一个ViewModel的绑定一个的IList< TagDTO>标签{搞定;组; } (形式往往随着时间的推移复杂,无论如何,所以一个视图模型几乎总是一个很好的路要走)。下面是在页面创建的HTML隐藏输入一个例子:

I have a website running with the option of adding tags, much like SO.
My approach to the problem, however, led to me create one input field for each added tag, and increment a javascript index variable every time a new input is added, then making use of a ViewModel to bind a IList<TagDTO> tags { get; set; } (forms tend to get complex over time anyway, so a viewmodel is almost always a good way to go). Here is an example of the html hidden inputs created in the page:

name=tags.Index, value=0
name=tags[0].tagid, value=201
name=tags.Index, value=2
name=tags[2].tagid, value=307

这有一个很大的优势,我:国际化标签,并可能不允许不存在变量结果
我的意思是,每一个标签都有一个ID,并在数据库中我的标签桌子上有对我支持的每种语言此标记的名称一列。如:

This has one great advantage to me: Internationalized tags and possibly disallowing nonexistent tags.
What I mean is that every tag has an ID, and in my "Tags" table in the database there is one column for the name of this tag in each language I support. Such as:

tagid | name_ptBR   | name_en
 201  | animais     | animals
 307  | educacional | educational

这只是我的方法解决问题,但它已确定到目前为止制定。

This is only my approach to the problem, but it has worked out ok so far.

这篇关于提交的JavaScript动态地添加元素像控制器#1的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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