59

テキスト フィールドが 2 つ以上のテキスト列で構成されている選択リストを生成するにはどうすればよいでしょうか。

Large--£200
Medium--£150
Small--£100

コントローラーコードは次のとおりです。

 var stands = db.Stands.Where(s => s.ExhibitorID == null).ToList();
 ViewBag.StandID = new SelectList(stands,"StandID", "Description" + "-- £" + "Rate");

...そして私の見解は(現在):

    <div class="editor-field"> 
        @Html.DropDownList("StandID", "--Select--") 
    </div> 

...しかし、「説明」+「--£」+「レート」); 実行されません:

DataBinding: 'System.Data.Entity.DynamicProxies.Stand_63F8C9F623B3C0E57D3008A57081AFCD9C39E1A6B79B0380B60840F1EFAE9DB4' には、'Description--£Rate' という名前のプロパティが含まれていません。

助けてくれてありがとう、

マーク

4

5 に答える 5

90

単純な LINQ プロジェクションを使用して新しい匿名クラスを作成し、SelectList(IEnumerable, string, string) コンストラクターのオーバーロード<option>を使用して、要素に使用する値とテキスト フィールドを指定できます。

var stands = 
  db.Stands
    .Where(s => s.ExhibitorID == null)
    .Select(s => new 
     { 
       StandID = s.StandID,
       Description = string.Format("{0}-- £{1}", s.Description, s.Rate) 
     })
    .ToList();

ViewBag.StandID = new SelectList(stands, "StandID", "Description")

編集

C#6 以降では、文字列補間により、より読みやすくなります。string.Format

   ...
   Description = $"{s.Description}-- £{s.Rate}"

ViewModel(匿名クラスではなく) 厳密なクラス名に射影する場合は、間違いなく魔法の文字列をnameof演算子の安全性に置き換えたいと思うでしょう:

ViewBag.StandID = new SelectList(stands, nameof(Stand.StandID), nameof(Stand.Description));
于 2012-10-04T12:31:15.207 に答える
15
var stands = db.Stands.Where(s => s.ExhibitorID == null).ToList();
IEnumerable<SelectListItem> selectList = from s in stands
                                         select new SelectListItem
                                                    {
                                                      Value = s.StandID,
                                                      Text = s.Description + "-- £" + s.Rate.ToString()
                                                    };
ViewBag.StandID = new SelectList(selectList, "Value", "Text");
于 2012-10-04T12:57:03.887 に答える
7

部分的な Model クラスを作成できます

public partial class Stand
{
    public string DisplayName
    {
        get
        {
            return this.Description + "-- £" + this.Rate.ToString();
        }
    }

}

次に、あなたのビューで

var stands = db.Stands.Where(s => s.ExhibitorID == null).ToList();
ViewBag.StandID = new SelectList(stands,"StandID", "DisplayName");
于 2016-03-04T20:14:44.557 に答える
4

使用しているコンストラクターの Format は SelectList(IEnumerable items, string dataValueField, string dataTextField) です。

したがって、あなたが持っている方法でそれを使用すると、実際には「Description-- £Rate」と呼ばれる TextField にバインドするように指示されます。これが DB から入ってくるフィールドと呼ばれるものではない場合、示しています。

上記の 2 つの方法のいずれかが機能するのは、dataValueField にある値が Value を入れるプロパティの名前と一致し、dataTextField が Text を入れる場所のプロパティ名と一致する限りです。おそらく 2 つの組み合わせです。上記のソリューション。(私が linq よりもラムダ式を好むという理由だけで。) selectlist 項目を使用すると、変換後にコレクションで ToList を実行する必要がなくなります。選択リストに自然にバインドされるオブジェクトを実際に作成しています。

また、リストに追加する前に、説明または料金をチェックして、それらが空でないことを確認することもできます。

var stands = db.Stands.Where(s => s.ExhibitorID == null)
                  .Select(s => new SelectListItem
                {
                    Value = s.StandID.ToString(),
                    Text = s.Description + "-- £" + s.Rate.ToString()
                });


ViewBag.StandID = new SelectList(stands, "Value", "Text");
于 2012-10-04T13:09:44.223 に答える
2

ビューモデルを変更することでこれを行いました。これが私のコードです:

ビューモデル

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MvcEsosNew.Models;
using System.Web.Mvc;

namespace MvcEsosNew.ViewModels
{
    public class EntitlementViewModel
    {
        public int EntitlementCount { get; set; }
        public Entitlement Entitlement { get; set; }
        public SelectList Member { get; set; }
        public SelectList Job_Grade { get; set; }
        public SelectList Department { get; set; }
        public SelectList Esos_Batch { get; set; }
    }

    public class department_FullName
    {
        public int deptID { get; set; }
        public string deptCode { get; set; }
        public string deptName { get; set; }
        public string fullName { get { return deptCode + " - " + deptName; } }
    }
}

コントローラー

public void getAllDepartment(EntitlementViewModel entitlementVM)
        {
            var department = from Department in db.Departments.Where(D => D.Status == "ACTIVE").ToList()
                             select new department_FullName
                             {
                                 deptID   = Department.id,
                                 deptCode = Department.department_code,
                                 deptName = Department.department_name
                             };
            entitlementVM.Department = new SelectList(department, "deptID", "fullName");
        }

景色

     <div class="form-group row">
                <div class="col-sm-2">
                    @Html.LabelFor(model => model.Entitlement.department_id)
                </div>
                <div class="col-sm-10">
                     @Html.DropDownListFor(model => model.Entitlement.department_id, Model.Department, new { @class="form-control" })
                     @Html.ValidationMessageFor(model => model.Entitlement.department_id)
                </div>
            </div>

結果:

結果

于 2017-12-11T03:29:11.393 に答える