1

ビューにを渡そうとしSelectListItemています。このチェックボックスリストは、日付範囲のあるJavaScriptダイアログボックスのビュー内で表示できます。

次のコードから、メソッドReadLogFileは正しいを返しますSelectListItem。このメソッドは、コントローラーのアクションメソッドから呼び出します。私のエンティティにRunDatesDisplayは、IEnumerableリストと呼ばれるプロパティが含まれています。System.Web.Mvcこのプロパティを選択リストアイテムとして作成することもできますが、ドメインプロジェクトで名前空間を参照したくありませんか?

問題は、選択リスト項目をエンティティ内のリストにキャストIEnumerableしてビューに渡すことができないことです。リストビューモデルを作成しました。ビュー内でエディターテンプレートを呼び出してチェックボックスをレンダリングします。

選択リスト項目をビューに正しく渡すためのヘルプが必要です。たとえば、をSelectListItem使用してモデルプロパティにを渡す必要がありますか、それとも最初にselectlistitemをselectlistにバインドする必要がありますか?ViewBagViewBag.RunDatesDisplay = items(selectlistitem)

実在物:

public class RunLogEntry
{

    public int ID { get; set; }

    public int RunID { get; set; }


    [NotMapped]
    public bool? LoadFileAttached { get; set; }

    [NotMapped]
    public bool? OutFileAttached { get; set; }

    [NotMapped]
    public string AttachedLoadListFileName { get; set; }

    [NotMapped]
    public string AttachedOutputFileName { get; set; }

    [NotMapped]
    public List<RunLogEntryTestExceptionDisplay> TestExceptionDisplay { get; set; }

    [NotMapped]
    public IEnumerable<RunLogEntryDatesDisplay> RunDatesDisplay { get; set; }
}

いくつかの処理を行い、selectlistitemを返すメソッド:

public static List<SelectListItem> ReadLogFile(List<string> fileNames)
{
    //Get collection of files and send to parser file by file:
    LogFile lf = new LogFile();


    var items = new List<SelectListItem>();
    foreach (var minDate in minDates)
    {
       var item = new SelectListItem() { Value = minDate.ToString(), Text = String.Format("{0} - {1}", minDate, minDate.AddMinutes(30)) };
        items.Add(item);
    }

    return items;
}

コントローラアクション(アクションのコード全体を含めませんでした)

//3. Send log file name collection to parser
if (logFiles != null && logFiles.Count > 0)
{
    var items = new List<SelectListItem>();

    items = FileImport.ReadLogFile(logFiles);
    **RunLogEntry.RunDatesDisplay = FileImport.ReadLogFile(logFiles);**

ViewModel:

public class RunLogRunDatesViewModel
{
    public IEnumerable<SelectListItem> RunLogRunDates { get; set; }
}

エディターテンプレート

@model RunLog.Domain.Entities.RunDatesDisplay 

<tr>
    <td>
        @Html.DisplayFor(x => x.RunDate)
    </td>
</tr>

そして最後に私の見解:

<div id="runDatestreeview" title="Dialog Title" style="font-size: 10px; font-weight: normal;
            overflow: scroll; width: 800px; height: 450px; display: none;">
    <table class="grid" style="width: 600px; margin: 3px 3px 3px 3px;">
        <thead>
            <tr>
                <th>
                    Run Dates:
                </th>
            </tr>
        </thead>
        <tbody>
            @if (Model.RunDatesDisplay != null)
            {
                @Html.EditorFor(x => x.RunDatesDisplay)
            }
        </tbody>
    </table>
</div>
4

1 に答える 1

1

2つのプロパティを使用してカスタムタイプを作成することもTuple、クラスを使用することもできるので、 ReadLogFile次のようになります。

public static List<Tuple<string,string>> ReadLogFile(List<string> fileNames)
{
   ...
   var items = new List<Tuple<string,string>>();
   foreach (var minDate in minDates)
   {
      var item = new Tuple<string, string>(minDate.ToString(), String.Format("{0} - {1}", minDate, minDate.AddMinutes(30));
      items.Add(item);
   }

   return items;
}

あなたのモデル:

public class RunLogRunDatesViewModel
{
       public SelectList RunLogRunDates { get; set; }
}

そしてあなたのコントローラー:

var model = new RunLogRunDatesViewModel();
List<Tuple<string,string>> items = FileImport.ReadLogFile(logFiles);
model.RunLogRunDates = new SelectList(items, "Item1", "Item2");

SelectListは、「Item1」から値を取得し、「Item2」からテキストを取得しようとします。

または、最初に言ったように、タプルの使用を避けるために2つのプロパティを持つ単純なクラスを作成できます

UPDATE1 .Net 2.0-3.5では、次のような単純なラッパーを作成できます。

public class Tuple<T1,T2>
{
    public T1 Item1 {get;set;}
    public T2 Item2 {get;set;}
}
于 2012-12-11T22:26:23.527 に答える