0

我有一个有点奇怪的情况,我在表单上动态生成了字段——所有下拉列表。这些选择对应于我想将它们相加以形成位掩码的二进制值。我以这种方式生成下拉列表:

<table class="center">
    @foreach (var field in Model.Fields)
    {
        <tr>
            <td>@field.DisplayText:</td>
            <td>
                @Html.DropDownList(field.FieldName, new SelectList(field.Options, "FlagValue", "Text", field.SelectedValue), "(doesn't matter)")                
            </td>
        </tr>
    }
</table>

这似乎可行——就在视图中呈现正确的 HTML 而言。但是我的控制器没有收到字段中的选择。我试着循环遍历动态字段。

在下面的代码中,PatientSelectorEditor 是我的 ViewModel。

    private void GetFlagInfo(PatientSelectorEditor pse, out string description, out long flags)
    {
        description = null;
        flags = 0;

        // get list of all possible fields that could be in the view.
        pse.Fields = InitPatientSelectorFields(0);
        foreach (PriceFlagField field in pse.Fields)
        {                
            foreach (var option in field.Options)
            {
                // was something selected here?
                if (Request[field.FieldName].Equals(option.FlagValue))
                {
                    description += ", " + option.Text;
                    flags += option.FlagValue;
                }
            }                
        }            
    }

走的线

 Request[field.Name]

没有在我的视图中找到动态生成的字段。

我究竟做错了什么?

4

1 回答 1

0

我发现我在这里做错了什么。这条线……

if (Request[field.FieldName].Equals(option.FlagValue))

需要像这样的显式字符串比较

if (Request[field.FieldName].Equals(option.FlagValue.ToString()))
于 2013-02-14T00:24:02.433 回答