【问题标题】:Make an Item Selected in dropdownlist在下拉列表中选择项目
【发布时间】:2016-01-01 21:14:04
【问题描述】:

我已经尝试了这个论坛上建议的几个解决方案,但没有一个对我有用。我可能做错了什么。 我有一个视图模型:

public class myViewModel
{
    public IEnumerable<StockStati> StockStatiList { get; set; }
    public IEnumerable<StagingOfferResponse> OfferList { get; set; }
  }

然后在我的控制器中,我正在填充 OfferList 并从数据库表中获取不同的库存状态:

List<StagingOfferResponse> stagResList = getOfferList();
StockStatiList =  (from s in oContext.StockStati
                                        select s).ToList();

StagingOfferResponse 表包含 StockStatus 字段,在视图中,我想使用 foreach 向用户显示所有打开的报价,然后能够从 dropDownlist 更改 StockStatus。但最初我希望在每一行的下拉列表中选择 StagingOfferResponse 的 StockStaus 字段的值。

查看:

 @foreach (StagingOfferResponse stRes in @Model.stagingOfferList)
  {
      <tr>
      <td>
          @Html.Label(stRes.StockCode)
          @Html.Hidden("StockCode_"+i,stRes.StockCode)
      </td>
          <td>
              @Html.TextBox("QtyOff_"+i,stRes.QtyOffered, new { @type = "number",@class="numberinput"  })
          </td>
          <td>
              @Html.TextBox("Price_"+i,stRes.Price, new { @type = "number" ,@class="numberinput" })
          </td>
          <td>
              @Html.DropDownListFor(x=>x.StockStatiList,new SelectList(@Model.StockStatiList, "SID", "StatusDescription")
              </td>

          </tr>
  }
       </table>

任何帮助。

谢谢

【问题讨论】:

  • 请显示更多代码
  • 请让我知道代码 sn-p 是否适合您。
  • 它只有在有一条记录要显示(一个报价)时才有效,而多个报价不起作用。

标签: c# asp.net-mvc drop-down-menu


【解决方案1】:

需要将选中项的ID传递给(SelectList):

@Html.DropDownListFor(x=>x.StockStatiList,new SelectList(@Model.StockStatiList, "SID", "StatusDescription",stRes.StockCode);

【讨论】:

  • 试过还是一样,我什至试过:@Html.DropDownListFor(x => x.SelectedStatusId, new SelectList(@Model.StockStatiList, "SID", "StatusDescription", stRes.StockStatus)) ;
  • (stRes) 应该包含一个属性,该属性的值映射到 (StockStatiList) 中的 ID 之一
  • 是的,它映射到 StockStatiList 中的 ID
  • 这个属性是什么?
  • 嗨,我尝试使用:@Html.DropDownListFor(x=>x.selectedID, new SelectList(@Model.StockStatiList, "SID", "StatusDescription", 2)) 仍然没有成功,不是确定我在这里做错了什么。
猜你喜欢
  • 1970-01-01
  • 2022-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-08
  • 1970-01-01
相关资源
最近更新 更多