2012-02-22 6 views
0

モデルにバインドする部分ビューをいくつか表示したビューがあります。何らかの理由で、私が投稿するとき、モデルは空であり、私はなぜそれがわからないのですか。ASP.NET MVC 3モデルにはポストバックに関する情報がありません

以下は私のViewModelです。

public class IndexViewModel 
{ 
    public bool AdvancedSearchOption { get; set; } 
    public bool ForceAdvanced { get; set; } 
    public bool ForceSimple { get; set; } 
    public string SimpleSearchCriteria { get; set; } 
    public string CustomerNumberCriteria { get; set; } 
    public string AccountNumberCriteria { get; set; } 
    public string NameCriteria { get; set; } 
    public string PhoneNumberCriteria { get; set; } 
} 

ここは私のコントローラです。値が部分的なビューになったかどうかを確認したいので、私はビューモデルのすべての値を埋めています。彼らはそこに着くので、私は問題を抱えています。ここで

public class HomeController : Controller 
{ 
    private ISecurityRepository SecurityRep; 

    public HomeController(ISecurityRepository repo) 
    { 
     SecurityRep = repo; 
    } 

    public ActionResult Index() 
    { 
     IndexViewModel temp = new IndexViewModel(); 
     temp.AdvancedSearchOption = SecurityRep.DefaultToAdvancedSearch(User.Identity.Name); 
     temp.ForceAdvanced = false; 
     temp.ForceSimple = false; 
     temp.SimpleSearchCriteria = "Testing"; 
     temp.AccountNumberCriteria = "Acct"; 
     temp.CustomerNumberCriteria = "Cust"; 
     temp.NameCriteria = "Name"; 
     temp.PhoneNumberCriteria = "Phone"; 
     return View(temp); 
    } 

    public ActionResult SimpleSearch() 
    { 
     IndexViewModel temp = new IndexViewModel(); 
     temp.AdvancedSearchOption = SecurityRep.DefaultToAdvancedSearch(User.Identity.Name); 
     temp.ForceAdvanced = false; 
     temp.ForceSimple = true; 
     temp.SimpleSearchCriteria = "Testing"; 
     temp.AccountNumberCriteria = "Acct"; 
     temp.CustomerNumberCriteria = "Cust"; 
     temp.NameCriteria = "Name"; 
     temp.PhoneNumberCriteria = "Phone"; 
     return View("Index",temp); 
    } 

    public ActionResult AdvancedSearch() 
    { 
     IndexViewModel temp = new IndexViewModel(); 
     temp.AdvancedSearchOption = SecurityRep.DefaultToAdvancedSearch(User.Identity.Name); 
     temp.ForceAdvanced = true; 
     temp.ForceSimple = false; 
     temp.SimpleSearchCriteria = "Testing"; 
     temp.AccountNumberCriteria= "Acct"; 
     temp.CustomerNumberCriteria= "Cust"; 
     temp.NameCriteria= "Name"; 
     temp.PhoneNumberCriteria = "Phone"; 
     return View("Index", temp); 
    } 

    [HttpPost] 
    public ActionResult Index(IndexViewModel vm, FormCollection formCollection) 
    { 
     return View(); 
    } 
} 

私の見解ここ

@model TRIOSoftware.Magnum.Models.IndexViewModel 

@{ 
    ViewBag.Title = "Search"; 
} 

@if ((@Model.AdvancedSearchOption && @Model.ForceSimple != true) || @Model.ForceAdvanced == true) 
{ 
    @Html.Partial("AdvancedSearch") 
} 
else 
{ 
    @Html.Partial("SimpleSearch") 
} 

は私のSimpleSearch部分図です。私はこれを働かせることができれば、他の人は同じ道をたどると思います。私は部分的に投稿を行い、それを行うにはjQueryを使用します。これらの事のいずれかが私に問題を引き起こすかどうかはわかりません。私は彼らが私の問題を引き起こしていないかどうかわからなかったので、私はそこに隠されたアイテムをすべて持っています。単純な検索部分ビューを表示するときにここで

@model TRIOSoftware.Magnum.Models.IndexViewModel 

<script type="text/javascript"> 
    $(document).ready(function() { 
     $("#DefaultDiv").find("#DefaultAdvanced").click(function() { 
      $.post("DefaultSimple"); 
     }); 

     $("#SearchSection").find("#SearchButton").click(function() { 
      $.post(""); 
     }); 
    }); 
</script> 

@using (Html.BeginForm("Index","Home")) 
{ 
    @Html.HiddenFor(m => m.ForceAdvanced) 
    @Html.HiddenFor(m => m.AdvancedSearchOption) 
    @Html.HiddenFor(m => m.ForceSimple) 
    @Html.HiddenFor(m => m.AccountNumberCriteria) 
    @Html.HiddenFor(m => m.CustomerNumberCriteria) 
    @Html.HiddenFor(m => m.NameCriteria) 
    @Html.HiddenFor(m => m.PhoneNumberCriteria) 

    <div id="DefaultDiv" style="float:right"> 
     <a id="DefaultAdvanced" href="#" class="ButtonClass">Default Simple Search</a> 
    </div> 

    <div style="clear:both; margin: auto; width: 800px"> 
     <img src="../../Content/images/TRIO_transparent_image.gif"; alt="TRIO Software"; style="margin-left:150px; clear:left"/> 
      <div style="clear:left; float: left" class="SearchText"> 
        @Html.Label("What's your inquiry?:") 
        @Html.EditorFor(m => m.SimpleSearchCriteria, new { style = "width: 400px" }) 
      </div> 
      <div id="SearchSection" style="float: left" class="SearchText"> 
       <a href="#"; id="SearchButton" class="ButtonClass"><img src="../../Content/images/Search.gif"; alt="Search"; style="float:left" /></a> 
      </div> 
      <p style="clear:left;margin-left:400px"> 
       @Html.ActionLink("Advanced Search", "AdvancedSearch", null, new { style = "clear:left" }) 
      </p> 

    </div> 
} 

はHTMLコードです:

<div id="main"> 
    <script type="text/javascript"> 
     $(document).ready(function() { 
      $("#DefaultDiv").find("#DefaultAdvanced").click(function() { 
       $.post("DefaultSimple"); 
      }); 

      $("#SearchSection").find("#SearchButton").click(function() { 
       $.post(""); 
      }); 
     }); 
    </script> 

    <form method="post" action="/"> 
     <input type="hidden" value="False" name="ForceAdvanced" id="ForceAdvanced" data-val-required="The ForceAdvanced field is required." data-val="true"> 
     <input type="hidden" value="False" name="AdvancedSearchOption" id="AdvancedSearchOption" data-val-required="The AdvancedSearchOption field is required." data-val="true"> 
     <input type="hidden" value="False" name="ForceSimple" id="ForceSimple" data-val-required="The ForceSimple field is required." data-val="true"> 
     <input type="hidden" value="Acct" name="AccountNumberCriteria" id="AccountNumberCriteria"> 
     <input type="hidden" value="Cust" name="CustomerNumberCriteria" id="CustomerNumberCriteria"> 
     <input type="hidden" value="Name" name="NameCriteria" id="NameCriteria"> 
     <input type="hidden" value="Phone" name="PhoneNumberCriteria" id="PhoneNumberCriteria"> 
     <div style="float:right" id="DefaultDiv"> 
      <a class="ButtonClass ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" href="#" id="DefaultAdvanced" role="button"><span class="ui-button-text">Default Simple Search</span></a> 
     </div> 
     <div style="clear:both; margin: auto; width: 800px"> 
      <img style="margin-left:150px; clear:left" alt="TRIO Software" ;="" src="../../Content/images/TRIO_transparent_image.gif"> 
      <div class="SearchText" style="clear:left; float: left"> 
       <label for="What_s_your_inquiry_:">What's your inquiry?:</label> 
       <input type="text" value="Testing" name="SimpleSearchCriteria" id="SimpleSearchCriteria" class="text-box single-line"> 
      </div> 
      <div class="SearchText" style="float: left" id="SearchSection"> 
       <a class="ButtonClass ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" id="SearchButton" ;="" href="#" role="button"><span class="ui-button-text"><img style="float:left" alt="Search" ;="" src="../../Content/images/Search.gif"></span></a> 
      </div> 
      <p style="clear:left;margin-left:400px"> 
       <a style="clear:left" href="/Home/AdvancedSearch">Advanced Search</a> 
      </p> 
    </div> 
    </form> 
</div> 

私はこの問題をどのように修正すればよいですか?

答えて

0

私は明示的に運を持つパーシャルをモデルに送信しようとしました。何も指定されていなければ、部分的なビューはデフォルトで親モデルを取得すると考えられます。したがって、私の必要とするのは、モデルの型を部分的に指定することだけでした。

私は最終的に多くの試行錯誤でそれを理解しました。私の問題は投稿を行うのにjQueryを使用しようとしているために発生していました。このようにしてモデルを更新するには、他にも必要なことがいくつかあります。私はそれを変更し、投稿のフォームに入力コントロールを置くと、コントローラの親ビューと部分ビューからすべてのデータを戻しました。

1

レンダリングする部分を選択していますが、モデルを渡すことはありません。あなたはそれにモデルを渡すことができます第二引数を取るHtml.Partialのオーバーロードされたバージョンがあります:

@Html.Partial("ViewName", Model); 

だからあなたの場合には、あなたはこの使用したい:

@if ((Model.AdvancedSearchOption && Model.ForceSimple != true) || Model.ForceAdvanced == true) 
{ 
    @Html.Partial("AdvancedSearch", Model) 
} 
else 
{ 
    @Html.Partial("SimpleSearch", Model) 
} 

をまた、私はどのように気づきますModelの接頭辞を付けた@を削除しました。理由をよりよく理解するには、Introduction to ASP.NET Web Programming Using the Razor SyntaxとPhil Haackによって書かれたこの同じトピックの小さな参照先hereを参照してください。

1

私は@ジョンヒが彼の答えで頭に釘を打つと思う。しかし、自分で作成した複雑さを減らすことができます。

1)ForceSimpleとForceAdvancedの両方がブール値であるため、ForceAdvancedがtrueの場合、「単純」ではないと思われます。私はあなたがここにいる他の論理が何であるか分かりません。

2)2つのビューを作成し、正しいものを取得するために「投稿」するのではなく、パラメータを使用して検索タイプを設定するだけではどうですか。または、セキュリティを評価して、ユーザーが実行できるセキュリティを設定します。ここでは例を示します。

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

//id is the search type: true is Advanced 
public ActionResult Search(bool id) { 
    IndexViewModel viewModel = new IndexViewModel { 
     /* Do whatever logic here */ 
     ForceAdvanced = (id) ? false : true, 
     AdvancedSearchOption = id 
    }; 
    return View("search", viewModel); 
} 

[HttpPost] 
public ActionResult Search(IndexViewModel model) { 
    //model.SimpleSearchCriteria = "Testing"; 
    //model.PhoneNumberCriteria = "Phone"; 
    return View("search", model); 
} 

検索ビュー:

@using (@Html.BeginForm(new { id = @Model.AdvancedSearchOption })) { 
    <div style="clear:left; float: left" class="SearchText"> 
     @Html.Label("What's your inquiry?:") 
     @if (Model.AdvancedSearchOption) { 
      <div> 
       @* if you really want, load your partial views here *@ 
       <span>@Html.LabelFor(m => m.NameCriteria)</span> 
       @Html.EditorFor(m => m.NameCriteria, new { style = "width: 400px" }) 
       <span>@Html.LabelFor(m => m.PhoneNumberCriteria)</span> 
       @Html.EditorFor(m => m.PhoneNumberCriteria, new { style = "width: 400px" }) 
      </div> 
     } 
     else { 
      @* if you really want, load your partial views here *@ 
      @Html.EditorFor(m => m.SimpleSearchCriteria, new { style = "width: 400px" }) 
     }   
    </div> 
    <div> 
     <input type="submit" value="Search" /> 
    </div> 
    @Html.HiddenFor(m => m.ForceAdvanced) 
    @Html.HiddenFor(m => m.AdvancedSearchOption) 
    @Html.HiddenFor(m => m.ForceSimple) 
    @Html.HiddenFor(m => m.AccountNumberCriteria) 
    @Html.HiddenFor(m => m.CustomerNumberCriteria) 
    @Html.HiddenFor(m => m.NameCriteria) 
    @Html.HiddenFor(m => m.PhoneNumberCriteria)  
} 
関連する問題