DropDownListFor()bindling值和文字视图模型 [英] DropDownListFor() bindling value AND text to viewmodel

查看:151
本文介绍了DropDownListFor()bindling值和文字视图模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个下拉列表()视图,与DropDownListFor实现形式。这种观点是强类型到视图模型,其中有一个的SelectList属性来保存下拉的选项,然后另一个属性持有下拉选定值。这是工作的罚款,但我想要做的是,同时按住所选的值,并在我的第二个属性的下拉中选定的文本。我想这样做的原因是,这样的形式selfposts,我有每个选择的文本和值。

我试图从int更改所选值属性一个KeyValuePair,但只有对的INT部分设置在表单提交。

也许有完全更好的方式来做到这一点,我向所有人开放的建议,包括我的方法的部分进行重新设计。

控制器(建筑的SelectList)

 的SelectList leadTypeGroups =新的SelectList(_enrollmentRepository.GetLeadTypeGroups(),键,值);

视图模型

 公共KeyValuePair< INT,串> LeadTypeGroupID {搞定;组; }
公众的SelectList LeadTypeGroups {搞定;组; }

查看

  @ Html.DropDownListFor(选择=> Model.LeadTypeGroupID,Model.LeadTypeGroups,)


解决方案

有断绝从每个请求的数据库(或内存中缓存)拉值。另外,与JavaScript的隐藏字段与适当的文字更新它时,下拉选择更改。

I have a form on a View with a dropdown list, implemented with DropDownListFor(). This View is strongly typed to a ViewModel, which has a SelectList property to hold the options of the dropdown, and then another property to hold the selected value of the dropdown. This is working fine, but what I'd like to do is, hold both the selected value AND selected text of the dropdown in my second property. The reason I want to do this is so that as the form selfposts, I have both the text and value of each selection.

I tried changing the selected value property from an int to a KeyValuePair but only the int part of the pair is set on form submission.

Perhaps there is a better way altogether to accomplish this, I am open to all suggestions including a partial redesign of my methods.

Controller (building SelectList)

SelectList leadTypeGroups = new SelectList(_enrollmentRepository.GetLeadTypeGroups(), "Key", "Value");

ViewModel

public KeyValuePair<int, string> LeadTypeGroupID { get; set; }
public SelectList LeadTypeGroups { get; set; }

View

@Html.DropDownListFor(selected => Model.LeadTypeGroupID, Model.LeadTypeGroups, " ")

解决方案

Have the sever pull the values from the database (or an in-memory cache) on each request. Alternatively, have a hidden field with javascript that updates it with the appropriate text whenever the dropdown selection changes.

这篇关于DropDownListFor()bindling值和文字视图模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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