MVC DropDown 选项值设置为多个逗号分隔值
有没有比我目前在这里所做的更好的方法: MVC 控制器操作创建一个选择列表:
ProductsDDL.Select(rp => new SelectListItem
{ Value = Model.RawMaterialID.ToString() + "," + plant, Text = Model.FinishedProductName });
And HTML rendered as:
<select id="Products" name="Products">
<option value="3,PLANT1">Finished Product1</option>
<option value="4,PLANT2">Finished Product2</option>
<option value="7,PLANT3">Finished Product3</option>
</select>
在选择更改时,我使用 Jquery $.GetJSON 填充另一个下拉列表。我将 PlantID 与 RawMaterialID 连接的原因是为了避免较长的查询处理时间。 在发布到操作(字符串 RawIDPlantID)时,我使用 Split(',') 来获取 RewMaterialID & PlantID
其他选项是使用会话来保存 PlantID 或在 MVC 视图中输入隐藏字段。
Is there any better approach than what I am currently doing here:
MVC Controller action creates a select list as:
ProductsDDL.Select(rp => new SelectListItem
{ Value = Model.RawMaterialID.ToString() + "," + plant, Text = Model.FinishedProductName });
And HTML rendered as:
<select id="Products" name="Products">
<option value="3,PLANT1">Finished Product1</option>
<option value="4,PLANT2">Finished Product2</option>
<option value="7,PLANT3">Finished Product3</option>
</select>
On selection change, I use Jquery $.GetJSON to populate another drop down list. The reason I am concatinating PlantID with RawMaterialID is to avoid long query processing time.
On Post to action(string RawIDPlantID), I use Split(',') to get RewMaterialID & PlantID
Other options are to use session to hold PlantID or input hidden field in MVC view.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我通常不使用逗号作为表示单个实体的值的分隔符,而是保存分隔多个 id 列表的逗号。在像你这样的情况下,我最终使用了下划线。然后,我是否需要用逗号分隔 ID 列表,这样更容易查看和解析。但这完全是主观的,取决于作为开发人员的您。
例如,
更容易引人注目
,这就是我对你的代码所能提出的尽可能多的批评。其余的我会使用,并且过去已经使用过。
I typically stay away from using commas as a delimiter in values that represent a single entity, and save the commas delimiting a list of multiple ids. In cases such as yours, I end up using an underscore. Then, should I need to comma separate a list of the IDs, it's easier on the eyes and to parse. But that's completely subjective and up to you, as the developer.
For example
is easier to eyeball
And that was as much criticism as I could muster about your code. The rest of it I'd use, and have used in the past.