2

最初に質問させてください。

ビューに表示する値のリストをロードする関数を呼び出す正しい場所はどこですか?

このようなコントローラーを作成します

public ActionResult Create()
{
    SeaModel newSea = new SeaModel();

    return View("Season/CreateSea", newSea);
}

//I not quite sure if this should go here or in another place
partial class seaDataContext
{
    public List<string> getSeaSettings()
    {
        var seaSettings = from p in settings
                          where p.setting == "periods"
                          select p.value;

        return seaSettings.ToList<string>();                              
    }
}

モデルはまるで

public class SeaModel
{
    [Required(ErrorMessage="*")]
    [Display(Name = "Period Name")]
    public string periods { get; set; }    
}

次のようなビューを作成します

  @using (Html.BeginForm()) {
        @Html.ValidationSummary(true, "Please correct the following errors.")

        <fieldset>
            <legend>Fields</legend>

            <div class="editor-label">
                @Html.LabelFor(model => model.periods)
            </div>
            <div class="editor-field">
                @Html.Select(model => model.periods, ****My doubt comes here****)
                @Html.ValidationMessageFor(model => model.periods)
            </div>

            <p>
                <input type="submit" value="Create" />
            </p>
        </fieldset>

    }

では、 getSeaSettings() の戻り値をビューに渡す方法と場所は?

ありがとう

4

3 に答える 3

4

ベストプラクティスは、このドロップダウンのモデルに選択リストを作成することです。

ただし、より簡単なオプションを使用することもできます。ViewData

 public ActionResult Create()
 {
     SeaModel newSea = new SeaModel();

     ViewData["myDropDown"] = new SelectList(listOfObjects, "valueOfTheObjectLikeID", "NameYouWantToShowInDropdown");

     return View("Season/CreateSea", newSea);
 }

それから:

@Html.Select(model => model.periods, ViewData["myDropDown"] as SelectList)

[HttpPost]メソッドで、検証が失敗した場合にviewdataも入力することを忘れないでください。そうすれば、ドロップダウンを再構築できます。

于 2010-11-03T14:13:15.410 に答える
2

Stefanvdsのアプローチは、私が以前行っていたものでした。
しかし、 を使用するより良い方法があることがわかりましたadditionalViewData

この EditorFor HTML ヘルパー拡張メソッドを使用します。
http://msdn.microsoft.com/en-us/library/ff406462.aspx

Select List Items をControllerのViewDataに渡す代わりに、 Viewでこれを行います。 パラメータの匿名オブジェクトとしてリスト項目を渡します。 重要なことは、プロパティ名と同じ名前を使用することです。

additionalViewData

@Html.EditorFor(
    m => m.MyPropertyName,
    new { MyPropertyName = Model.ListItemsForMyPropertyName }
);


もちろん、View Model オブジェクトを渡しています。

public class MyViewModel
{
    public int MyPropertyName;

    public IList<SelectListItem> ListItemsForMyPropertyName;
}



EditorForメソッドは、既存の Editor View Templates を使用します
そのため、Html.DropDown( ) メソッドを使用する場合のように、CSS クラス名と HTML 属性を再度指定する必要はありません。

例えば、

//------------------------------
// additionalViewData
//------------------------------
@Html.EditorFor(
    m => m.MyPropertyName,
    new { MyPropertyName = Model.ListItemsForMyPropertyName }
)

//------------------------------
//  traditional approach requires to pass your own HTML attributes
//------------------------------
@Html.DropDown(
    "MyPropertyName",
    Model.ListItemsForMyPropertyName,
    new Dictionary<string, object> {
        { "class", "myDropDownCssClass" }
    }
);

//------------------------------
//  DropDownListFor still requires you to pass in your own HTML attributes
//------------------------------
@Html.DropDownListFor(
    m => m.MyPropertyName,
    Model.ListItemsForMyPropertyName,
    new Dictionary<string, object> {
        { "class", "myDropDownCssClass" }
    }
);

それが私がadditionalViewDataアプローチをより好む理由です。
レンダリングされる HTML コードは、完全にエディター テンプレートに依存しているためです。

また、専用のビュー モデルを使用すると、コードがよりクリーンになり、保守が容易になります。

それが役に立てば幸い。

于 2010-11-04T17:36:42.247 に答える
2

リポジトリのパターンを確認する必要があります。このチュートリアルをasp.netサイト http://www.asp.net/mvc/tutorials/creating-model-classes-with-linq-to-sql-csでご覧ください

于 2010-11-03T18:12:46.487 に答える