5

Web アプリケーションで ViewModel を正しく使用するために最善を尽くしていますが、さまざまな問題が発生しています。そのうちの 1 つは、Create アクションを使用して投稿した直後にブレークポイントを設定すると、viewModel にフォームの値が保存されていないことです。私は何か間違ったことをしているに違いありませんが、いくつかのことを試しました。以下のコードを含めて、フォーム項目に viewModel フィールドと同じ名前を付けて、それが役立つかどうかを確認します。

また、ビューモデルのプロパティが正確に何を表す必要があるのか​​も疑問に思っています。人々がブログの投稿などでさまざまなものを使用しているのを見てきました。

ビューが選択リストをレンダリングする場合、ビューモデルは以下のように IEnumerable SelectListItem を保持する必要があるという印象を受けています。それでも、選択リストが表す型を表すために、代わりに IEnumerable Entity を使用する人を見てきました。

誰かが私のためにこれに光を当てることができますか? 昨夜、ビジネスロジック全体を破棄して、最初からやり直して正しく実行できるようにしました。

私のビューモデル:

public class ServerCreateViewModel
{
    public int Id { get; set; }

    // CompanyName represents a field in the Company model. I did this to see if
    // it would help with model binding. Beforehand it was Companies to represent the type. I've done the same for the rest of them, so I wont comment on this again.
    public IEnumerable<SelectListItem> CompanyName { get; set; }

    // Represents the Game model.
    public IEnumerable<SelectListItem> GameTitle { get; set; }

    //Represents the Location model, etc...
    public IEnumerable<SelectListItem> City { get; set; }
    public IEnumerable<SelectListItem> NumberOfPlayers { get; set; }
    public IEnumerable<SelectListItem> CurrencyAbbreviation { get; set; }
}

私のコントローラーアクション:

    public ActionResult Create()
    {
        var viewModel = new ServerCreateViewModel();

        viewModel.CompanyName = new SelectList(_dataService.Companies.All(), "Id", "CompanyName");
        viewModel.GameTitle = new SelectList(_dataService.Games.All(), "Id", "GameTitle");
        viewModel.City = new SelectList(_dataService.Locations.All(), "Id", "City");
        viewModel.NumberOfPlayers = new SelectList(_dataService.ServerPlayers.All(), "Id", "NumberOfPlayers");

        return View(viewModel);
    } 

    [HttpPost]
    public ActionResult Create(FormCollection collection, ServerCreateViewModel viewModel)
    {
        try
        {      // I put a breakpoint in here to check the viewModel values.
               // If I dont pass the viewModel into the constructor, it doesnt exist.
               // When I do pass it in, its empty.
            return Content("Success");
        }
        catch
        {
            return Content("Fail");
        }
    }  

私の見解:

@model GameserverCompare.ViewModels.Server.ServerCreateViewModel


@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<fieldset>
    <legend>Server</legend>
    @Html.HiddenFor(m => m.Id)
     <div class="editor-label">
        @Html.LabelFor(model => model.CompanyName)
    </div>
    <div class="editor-field">
        @Html.DropDownListFor(m => Model.CompanyName, Model.CompanyName)
        @Html.ValidationMessageFor(model => model.CompanyName)
    </div>
     <div class="editor-label">
        @Html.LabelFor(model => model.GameTitle)
    </div>
    <div class="editor-field">
        @Html.DropDownListFor(m => Model.GameTitle, Model.GameTitle)
        @Html.ValidationMessageFor(model => model.GameTitle)
    </div>
    <div class="editor-label">
        @Html.LabelFor(model => model.City)
    </div>
    <div class="editor-field">
        @Html.DropDownListFor(m => Model.City, Model.City)
        @Html.ValidationMessageFor(model => model.City)
    </div>
    <div class="editor-label">
        @Html.LabelFor(model => model.NumberOfPlayers)
    </div>
    <div class="editor-field">
        @Html.DropDownListFor(m => Model.NumberOfPlayers, Model.NumberOfPlayers)
        @Html.ValidationMessageFor(model => model.NumberOfPlayers)
    </div>
    <p>
        <input type="submit" value="Create" />
    </p>

</fieldset>
}   
4

1 に答える 1

4

フォームモデルでSelectListプロパティを使用しているため、これらのリストで選択された値を表すには、別のモデルが必要になります。

public class ServerCreatePostbackModel
{
    public int Id { get; set; }

    // CompanyName represents a field in the Company model. 
    public string CompanyName { get; set; }

    // Represents the Game model.
    public string GameTitle { get; set; }

    //Represents the Location model, etc...
    public string City { get; set; }
    public int NumberOfPlayers { get; set; }
    public string CurrencyAbbreviation { get; set; }
}

HttpPostアクションにこれらのいずれかを引数として使用させます。

ああ、そして必ずプロパティに使用HiddenForしてIdください。そうすれば、他のデータと一緒に返送されます。

于 2011-10-22T08:20:32.560 に答える