2010-11-28 16 views
1

ちょっと、私はMVC2のヘルプデスクで作業しています。私のビューからViewModelsにデータが転送されないという問題があります。ViewModelでデータが転送されないのはなぜですか?

ホームページには、問題のある地域を選択できる3つのDropDownListsがあります。その後、ボタンを押して問題を報告するか、FAQに移動します。 DropDownListsで問題のある領域を選択した場合、サイトはviewmodelでデータ転送を行うことで選択した内容を覚えておく必要があります。

ホームインデックスのPostメソッドを取得すると、SelectListsとSelectListItemsはnullになりますが、ボタン情報を格納する文字列には正しいデータが含まれています。

私は間違っていますか?

これは私のホームコントローラです:

public ActionResult Index() 
    { 
     var viewModel = new HomeIndexViewModel() 
     { 
      // The lists with problem areas are populated. 
      problemAreas1 = new SelectList(helpDeskRepository.GetProblemAreas(), "ProblemAreaID", "ProblemAreaName"), 
      problemAreas2 = new SelectList(helpDeskRepository.GetProblemAreas(), "ProblemAreaID", "ProblemAreaName"), 
      problemAreas3 = new SelectList(helpDeskRepository.GetProblemAreas(), "ProblemAreaID", "ProblemAreaName") 
     }; 
     return View(viewModel); 
    } 

    // 
    // POST: /Home/ 

    [HttpPost] 
    public ActionResult Index(HomeIndexViewModel viewModel) 
    { 
     // snip 
    } 

私のviewmodel:

public class HomeIndexViewModel 
{ 
    // A SelectList containing elements for the DropDownLists in the Home "Index" view. 
    public SelectList problemAreas1 { get; set; } 
    public SelectList problemAreas2 { get; set; } 
    public SelectList problemAreas3 { get; set; } 

    // Items chosen in the DropDownLists. 
    public SelectListItem itemOne { get; set; } 
    public SelectListItem itemTwo { get; set; } 
    public SelectListItem itemThree { get; set; } 

    // String for IDing what button is pressed. 
    public string submitButton { get; set; } 
} 

そしてHomeIndexViewModelから継承私の家インデックスビュー(それは、ねじアップするので、私はコードが含まれていませんでしたポスト):

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> 
<% using (Html.BeginForm("Index", "Home", FormMethod.Post)) 
    {%> 
<h2> 
    Search for your problem:</h2> 
<asp:Label ID="Label1" runat="server" Width="300px"> 
Choose a problem area, and search for an answer to your problem in the FAQ, 
    or you can press the report button to report your problem.</asp:Label> 
<%-- Here are the DropDownLists. --%> 
<p style="width: 220px; height: 24px;"> 
    <%: Html.DropDownListFor(model => Model.itemOne, Model.problemAreas1, "Choose a problem area")%> 
</p> 
<p style="width: 220px;"> 
    <%: Html.DropDownListFor(model => Model.itemTwo, Model.problemAreas2, "Choose a problem area")%> 
</p> 
<p style="width: 220px"> 
    <%: Html.DropDownListFor(model => Model.itemThree, Model.problemAreas3, "Choose a problem area")%> 
</p> 
<p> 
    <%-- Here are the two buttons allowing people to do a search or create a new ticket respectively. --%> 
    <input type="submit" value="Search for problems" name="submitButton" id="searchButton" /> 
    <input type="submit" value="Report problem" name="submitButton" id="submitButton" /> 
</p> 
<%} %> 

ありがとうございました。 :-)

答えて

3

フォームをPOSTアクションに戻すと、その内容が決して転記されないためproblemAreas1,2,3プロパティがnullになることは完全に正常です。あなたはGETアクションで行ったのと同じ方法でそれらを再移植する必要があります。 itemOne、Two、Threeプロパティに関しては、単純な文字列値であり、タイプがSelectListItemではないはずです。

モデル:

public class HomeIndexViewModel 
{ 
    // A SelectList containing elements for the DropDownLists in the Home "Index" view. 
    public SelectList problemAreas1 { get; set; } 
    public SelectList problemAreas2 { get; set; } 
    public SelectList problemAreas3 { get; set; } 

    // Items chosen in the DropDownLists. 
    public string itemOne { get; set; } 
    public string itemTwo { get; set; } 
    public string itemThree { get; set; } 

    // String for IDing what button is pressed. 
    public string submitButton { get; set; } 
} 

コントローラー:

public ActionResult Index() 
{ 
    var viewModel = new HomeIndexViewModel() 
    { 
     // The lists with problem areas are populated. 
     problemAreas1 = new SelectList(helpDeskRepository.GetProblemAreas(), "ProblemAreaID", "ProblemAreaName"), 
     problemAreas2 = new SelectList(helpDeskRepository.GetProblemAreas(), "ProblemAreaID", "ProblemAreaName"), 
     problemAreas3 = new SelectList(helpDeskRepository.GetProblemAreas(), "ProblemAreaID", "ProblemAreaName") 
    }; 
    return View(viewModel); 
} 

// 
// POST: /Home/ 

[HttpPost] 
public ActionResult Index(HomeIndexViewModel viewModel) 
{ 
    if (!ModelState.IsValid) 
    { 
     // the model wasn't valid => 
     // show the same form so that the user can fix validation errors 
     viewModel.problemAreas1 = new SelectList(helpDeskRepository.GetProblemAreas(), "ProblemAreaID", "ProblemAreaName"); 
     viewModel.problemAreas2 = new SelectList(helpDeskRepository.GetProblemAreas(), "ProblemAreaID", "ProblemAreaName"); 
     viewModel.problemAreas3 = new SelectList(helpDeskRepository.GetProblemAreas(), "ProblemAreaID", "ProblemAreaName"); 
     return View(viewModel); 
    } 
    // TODO: everything went fine => update your database and redirect 
    return RedirectToAction("Index"); 
} 
+0

ええ、それは私が逃したものです。文字列値、Int値、Guid値、またはいずれかの値の種類。 –

+0

DropDownListForをDropDownListForに変更する前に、それがあったのです。文字列を使用してボタンを押してFAQを検索すると(リストの設定は私の家のインデックスビューと同じです)、この例外が発生します: キー 'itemOne'を持つViewDataアイテムは 'System'タイプです。文字列 'である必要がありますが、' IEnumerable '型である必要があります。 私はまず、それらをSelectListItemsに変更しました。 – KasMA1990

+0

待ち伏せ。私はFAQコントローラにDropDownListsを設定していませんでした。助けてくれてありがとう、それは今働いている。 :-) – KasMA1990

0

自分のモデルについて知っていると思うページディレクティブは表示されません。そのデータはバック形式で渡されていないため、

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<HomeIndexViewModel>" %> 
+0

はええ、私はコードセクションでそれを追加しようとしましたが、それはそう、私はそれを左ポストをめちゃくちゃ。私はそれを投稿する前に情報を追加するのを忘れて、30秒後に編集を行った。私はそこにそれを持っている。 :)しかし、ありがとう。 :) – KasMA1990

+0

あなたのデバッグ時にあなたのviewModelには実際に想定されているオブジェクトも含まれていますか?あなたのコードは上手く見えるので、おそらくあなたのモデルは空です。 – Slee

+0

ええ、私がデバッグしてviewmodelを開くと、すべてがそこにあります。しかし、 "submitButton"はnullではない唯一のものです。 – KasMA1990

0

あなたの選択リストがnullになります、[OK]を次のようになります。このような

何かがあなたのビューの最初の行でなければなりません。しかし、それらはnullであるため、MVCは返すコレクション内で見つけることができるSelectListItemがないため、nullでもあります。私は、バインダーはSelectListsであなたのHomeIndexViewModelが作成されますリターンPOSTに起こるのだろうと信じて何HomeIndexViewModelの代わりに、コントローラでのあなたのコンストラクタに以下を追加

てみてください(ちょうど何が起こるかを見るために)

problemAreas1 = new SelectList(helpDeskRepository.GetProblemAreas(), "ProblemAreaID", "ProblemAreaName"), 
problemAreas2 = new SelectList(helpDeskRepository.GetProblemAreas(), "ProblemAreaID", "ProblemAreaName"), 
problemAreas3 = new SelectList(helpDeskRepository.GetProblemAreas(), "ProblemAreaID", "ProblemAreaName") 

人口密度が高く、あなたのSelectListItemを入力することができます。

何か少し怪しいようです。私のガット本能は、SelectListItemがヌルであってはならないと私に伝えています。

+0

さて、私はそれを試みました、そして、私のSelectListsは、彼らがすべきであるが、私のSelectListItemsはまだnullです。また、ここでボタンの1つを押すと、このオブジェクトのパラメータがないコンストラクタがないことを示すMissingMethodExceptionが発生します。これは、SelectListItemsからnull入力があるためコードが停止する前です。 – KasMA1990

+0

'HomeIndexViewModel'の代わりにコンストラクタをコントローラに追加したか、' HomeIndexViewModel'のコンストラクタがパラメータレスでないように思えます。 –

+0

HTML Select要素の1つの出力を使って質問を更新することもできますか? –

関連する問題