根据MVC中的字符串数组选择选项值

时间:2013-08-19 07:41:33

标签: asp.net-mvc html-select

我的View中有一个for循环,它会生成一个Dropdownlist。该循环迭代数组SessionData.ddlCondition.Length

的值
for(int i = 0; i < SessionData.ddlCondition.Length; i++)
            {
                <select class="ddlList">
                    <option value="Uwi" datatype="String">Uwi</option>
                    <option value="Uwi2" datatype="String">Uwi2</option>
                    <option value="UwiType" datatype="Decimal">UwiType</option>
                    <option value="Uwi2Type" datatype="Decimal">Uwi2Type</option>
                    <option value="Name" datatype="String">Name</option>
                    <option value="SiteType" datatype="Decimal">SiteType</option>
                    <option value="EffectiveDate" datatype="Date">EffectiveDate</option>
                    <option value="EffectiveEndDate" datatype="Date">EffectiveEndDate</option>
                    <option value="OilAllocationPlace" datatype="Decimal">OilAllocationPlace</option>
                    <option value="GasAllocationPlace" datatype="Decimal">GasAllocationPlace</option>
                    <option value="WaterAllocationPlace" datatype="Decimal">WaterAllocationPlace</option>
                    <option value="CondyAlloationPlace" datatype="Decimal">CondyAlloationPlace</option>

                    <option value="AffectsParentOilProd" datatype="Decimal">AffectsParentOilProd</option>
                    <option value="AffectsParentWaterProd" datatype="Decimal">AffectsParentWaterProd</option>
                    <option value="AffectsParentGasProd" datatype="Decimal">AffectsParentGasProd</option>
                    <option value="AffectsParentCondyProd" datatype="Decimal">AffectsParentCondyProd</option>   
                    <option value="OilConnectionParent" datatype="Decimal">OilConnectionParent</option>
                    <option value="GasConnectionParent" datatype="Decimal">GasConnectionParent</option>
                    <option value="WaterConnectionParent" datatype="Decimal">WaterConnectionParent</option>
                    <option value="CondyConnectionParent" datatype="Decimal">CondyConnectionParent</option>

                    <option value="OilSatellite" datatype="Decimal">OilSatellite</option>
                    <option value="GasSatellite" datatype="Decimal">GasSatellite</option>
                    <option value="WaterSatellite" datatype="Decimal">WaterSatellite</option>
                    <option value="CondySatellite" datatype="Decimal">CondySatellite</option>

                    <option value="GOR" datatype="Decimal">GOR</option>
                    <option value="UseGor" datatype="Boolean">UseGor</option>
                    <option value="Notes" datatype="String">Notes</option>

                    <option value="OpFlag" datatype="Decimal">OpFlag</option>
                </select>
            }

ddlCondition声明为string [] ddlCondition。这会将此下拉列表中的选定值存储在Controller中。

string[0] =  "Uwi"
string[1] =  "Uw2"
string[2] = "UwiType"
string[3] = "Uwi2Type"
................
................

现在,我想在我的View for string [2]中选择该选项。我该怎么办?

1 个答案:

答案 0 :(得分:0)

您应该对视图进行以下更改,即为您的下拉菜单命名

 var name = "ddlCondition[" + @i.ToString() + "]";
 <select class="ddlList" name='@name'>

并在你的行动中

public ActionResult MyAction(string []ddlCondition)

这样,您就会在ddlCondition中获得发布的值,并且可以使用ddlCondition[2]访问相应下拉菜单的值,依此类推