【问题标题】:Set selected value on Html.DropdownListFor from mode asp.net mvc从模式 asp.net mvc 在 Html.DropdownListFor 上设置选定的值
【发布时间】:2021-11-21 06:09:45
【问题描述】:

我无法根据我选择的系统在下拉菜单中设置选择的值。我尝试了我看到的其他解决方案,但仍然无法解决我的问题。

这是我的代码。

型号

[Serializable]
public class IssuesModel
{
  public int ISSUE_ID {get; set;}
  public string SYSTEMNAME {get; set;}
  public string ISSUE_DESC {get; set;}
  .......
}

[Serializable]
public class IssueDetailViewModel
{
  public IssuesModel Issue {get; set}
  public ICollection<SystemSummaryViewModel> Systems {get; set;} = new List<SystemSummaryViewModel>();
  public SelectedSystemModel SelectedSystem {get; set;}
}

[Serializable]
public class SystemSummaryViewModel
{
  public int ID {get; set;}
  public string SYSTEMNAME {get; set;}
}

[Serializable]
public class SelectedSystemModel
{
  public string SYSTEMNAME {get; set;}
}

控制器

[HttpGet]
public ActionResult EditIssue(int id = 0)
{
  var selectedIssue = db.TBL_ISSUES
     .Where(x => x.ISSUE_ID == id)
     .Select(x => new IssueModel
     {
       ISSUE_DESC = x.ISSUE_DESC,
       SYSTEMNAME = x.SYSTEMNAME
       ......
     }).FirstOrDefault();

  var systems = db.LIB_SYSTEMS
    .Where(x => x.ENABLED == "Y")
    .Select(x => new SystemSummaryViewModel
     {
       ID = x.ID,
       SYSTEMNAME = x.SYSTEMNAME
     }).ToList();

  var selectedSystem = db.TBL_ISSUES
    .Where(x = x.ISSUE_ID == id)
    .Select(x => new SelectedSystemModel
    {
      SYSTEMNAME = x.SYSTEMNAME
    }).FirstOrDefault();

  var model = new IssueDetailViewModel
  {
    Issue = selectedIssue,
    Systems = systems,
    SelectedSystem = selectedSystem
  }
};
return View(model);

查看

@model ProjectName.Models.IssueDetailViewModle
@{
  Layout = null;
}
....
<div class="form-group">
  @{
     var systemList = new List<SelectedListItem>();
     foreach(var item in Model.Systems)
     {
       systemList.Add(new SelectListItem() { Text = item.SYSTEMNAME, Value = item.SYSTEMNAME}); //this is my current in populating dropdown values
     }
  }

  @Html.DropDownListFor(m => m.SelectedSystem.SYSTEMNAME, systemList, new { @class="control-label"})
//also tried below but no luck
@*
   Html.DropDownListFor(m => m.SelectedSystem.SYSTEMNAME, new SelectList(Model.Systems, "SYSTEMNAME", "SYSTEMNAME", Model.SelectedSystem), "Select System", new { @class ="form-control"}
*@
</div>

我只得到下拉列表,但没有默认选中项。 希望你能帮我解决这个问题。非常感谢您,祝您有美好的一天

【问题讨论】:

    标签: asp.net-mvc model-view-controller razor


    【解决方案1】:

    @Html.DropDownListFor 对选中的项目很棘手,试试这个

    @Html.DropDownListFor(m => m.SelectedSystem.SYSTEMNAME, 
    new SelectList(Model.Systems, "SYSTEMNAME", "SYSTEMNAME", 
    Model.SelectedSystem.SYSTEMNAME), "Select System", new { @class ="form-control"}
    

    所以我更喜欢asp.net select

    @{
         var systemList = Model.Systems.Select(item=> new SelectListItem 
         { Text = item.SYSTEMNAME, 
           Value = item.SYSTEMNAME
          }).ToList();; 
    }
    
    ......
    
    <select class="form-control" asp-for="SelectedSystem.SYSTEMNAME" asp-items="@systemList"></select>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-21
      • 1970-01-01
      • 2012-06-20
      • 2013-10-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多