【问题标题】:DropDownList - How to add SelectListItem ASP.NET MVCDropDownList - 如何添加 SelectListItem ASP.NET MVC
【发布时间】:2018-06-21 11:16:23
【问题描述】:

我有一个 DropDownList ,它显示状态列表,但是当我从 DropDownlist 中选择一个项目并且我检查了 HTML 标记时,我可以看到没有 Selected attr 并且我研究并发现我需要在我的 Controller 中使用 SelectListItem 和比我尝试在我的控制器中实现它,但我遇到了一些错误:) 通常我在我的 Razor 视图(静态)中实现 DropDown,但这次是第一次 :) 我想在我的控制器中实现它,所以它变成动态的。
谁能指出我正确的方向:)
在此先感谢:)

控制器:

    //DropDown
    public List<VMRMA.NameStatusDropDown> GetStatusForDropDown()
    {

        List<VMRMA.NameStatusDropDown> result = new List<VMRMA.NameStatusDropDown>();

        var obj = db.RMAStatus.Select(u => u).ToList();


        if (obj != null && obj.Count() > 0)
        {
            foreach (var data in obj)
            {
                VMRMA.NameStatusDropDown model = new VMRMA.NameStatusDropDown();
                model.Status = data.Status;
                model.ID = data.ID;
                result.Add(model);
            }
        }

        return result;

   }

    //Dropdown runs in this Action
    public ActionResult RMA ()
    {
      VMRMA model = new VMRMA();
      model.NameStatusDropDowns = GetStatusForDropDown();

     //RMA query and some an other stuff

     return View(model);

    }

视图模型:

    public class VMRMA
    {

        public List<NameStatusDropDown> NameStatusDropDowns { get; set; }


        //DropDown
        public class NameStatusDropDown
        {
            public NameStatusDropDown()
            {

            }
            public NameStatusDropDown(int ID, string Status)
            {
                this.ID = ID;
                this.Status = Status;


            }

            public int ID { get; set; }


   public string Status { get; set; }


    }

}

查看:

@using ModelNamespace.Models
@model VMRMA

<form>
<div class="form-group">
<label class="form-control-label">Select a status</label>
<br />
<select>

<option>Select</option>
@foreach (var item in Model.NameStatusDropDowns)
{                                        
  <option value="@item.ID">@item.Status</option>
}
</select>

</div>
  <div class="form-group">
  <input type="submit" value="Send data" class="btn btn-primary">                                 
 </div>

</form>

HTML 标记:

<div class="form-group">
<label class="form-control-label">Select a status</label>
<br>
 <select>
<option>Select</option>
<option value="1">Sendt</option>
<option value="2">Under behandling</option>
<option value="3">Blive behandlet</option>
<option value="4">Modtaget</option>
</select>

</div>

【问题讨论】:

  • 为什么要手动创建 html(您的 &lt;select&gt; 甚至没有 name 属性,所以不会发回任何内容)。始终使用类型为DropDownListFor() 的字符串方法。典型示例参考this Q/A或正确绑定&lt;select&gt;
  • 嗨@StephenMuecke :) :) 我试过这样得到错误,我知道我错过了什么,请你纠正我:) @Html.DropDownListFor(Model.NameStatusDropDowns,IEnumerable)
  • 阅读我给你的链接(你评论中的代码远不接近正确)
  • @StephenMuecke,我读过它,它很棒,但它是关于一些错误 :) 我不明白 :) :) :) 有什么办法可以告诉我如何修复

标签: c# asp.net asp.net-mvc


【解决方案1】:

这两个帖子帮助我解决了这个问题,感谢@Stephen Muecke 的好帖子,写在Here 和感谢这篇带有很好解释的帖子,写在Here
这是我所做的,也许有一天它对某人有所帮助:) :

将属性添加到我的视图模型:

public class VMRMA
{
        public List<SelectListItem> Status { set; get; }

        public int? SelectedStatus { set; get; }   
}

将我的 ActionResult 更改为:

public ActionResult RMA (int Id)
    {
        VMRMA model = new VMRMA();

        model.Status = new SelectList(DatabaseNameSpace.RMAStatus, "ID", 
        "Status").ToList();
        //some an other stuff

       return View(model);
    }

然后将我的视图更改为:

@Html.DropDownListFor(s => s.SelectedStatus, Model.Status, "- Select -", new { @class = "form-control" }) 

【讨论】:

    【解决方案2】:

    控制器:

    ViewBag.Statuses= new SelectList(_context.RMAStatus
                    .Select(item => new { value = item.Id, text = item.Status}), "value", "text", selectedId);
    

    查看:

    @Html.DropDownListFor(x => x.StatusId, ViewBag.Statuses as SelectList, "- please select -")
    

    【讨论】:

      【解决方案3】:

      这样创建局部视图:

          @model MyApp.Models.MyClass
      
      
      @{
          Layout = null;
      }
      @*@Html.Partial("ActionMethod", "Controller", new ViewDataDictionary { { "Name", "TestName" } })*@
      
      @Html.DropDownList((String)TempData["Name"], new SelectList( ViewBag.Specialities,"Value","Text"),
          new { @class = "form-control", @multiple="multiple" });
      

      然后在你的控制器中

       List<MyClass> lstSpecialities = 
              ViewBag.Specialities = lstSpecialities; // Now it is available for the view
      

      最后一步,使用 @Html.RenderAction() 加载您的视图

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-22
        • 1970-01-01
        • 2016-12-10
        • 2017-08-24
        • 1970-01-01
        • 2018-12-16
        相关资源
        最近更新 更多