2016-10-02 14 views
-1

私はVS 2013 Professionalを使用してMVCのメインビューにサブビューからデータを渡そうとしています。私は主な言語としてC#とRazorを使用しています。サブビューの情報をメインビューに動的に投稿しようとしていますが、どうやってそれを行うのかは分かりません。今、私はそうのようなデータをポストするためにJavaScriptを使用しています:サブビューからメインビューにMVCでPOSTされたデータを取得するには?

#partialViewList# 
    @using WebApplication2.Models 
    @model IEnumerable<WebApplication2.Models.OBJECT> 
    <script> 
     function postItO(OfficeLocation, Name, Email, Phone, NumComputers, NumMonitors) { 

     form = document.createElement('form'); 
     form.setAttribute('method', 'POST'); 
     form.setAttribute('action', 'CompAndMon'); 
     myvar = document.createElement('input'); 
     myvar.setAttribute('OfficeName', "Primary Contact Name " + Name); 
     myvar.setAttribute('type', 'Office'); 
     myvar.setAttribute('Email', "Primary Contact Email: " + Email); 
     myvar.setAttribute('Phone', "Primary Contact Phone: " + Phone); 
     myvar.setAttribute('NumComps', "Number of Computers: " + NumComputers); 
     myvar.setAttribute('NumMons', "Number Of Monitors: " + NumMonitors); 
     form.appendChild(myvar); 
     document.body.appendChild(form); 
     form.submit(); 
    } 
    //I use the same general Idea when posting the other two models but so the code is not too long I will exclude them 
    </script> 

    <div class="panel-body col-lg-11 col-lg-offset-1" style="overflow-y: scroll;"> 




     @foreach (var item in Model) 
     { 

      if (@item.Type == 1) 
      { 

       var office = item as Office; 
       string loc = @office.OfficeName; 
       string Name = @office.OfficeNumber; 
       string email = @office.ContactName; 
       string phone = @office.ContactNumber; 
       // int mons = @office.NumMonitors; 
       //int comps = @office.NumComputers; 

       <p><a onclick="postItO('@loc','@Name','@email','@phone')">@office.OfficeName</a></p> 
      } 
      else if (@item.Type == 2) 
      { 
       var computer = item as Computer; 
       string lst = @computer.LastUser; 
       string Nme = @computer.Name; 
       string TtHD = @computer.TotalHDSpace; 
       int NmUp = @computer.NumUpdates; 
       int NmMn = @computer.NumMonitors; 
       string FrHD = @computer.FreeHDSpace; 

       <p>&ensp;&ensp;<a onclick="postItC('@lst','@Nme', '@TtHD','@FrHD','@NmMn','@NmUp')">@computer.Name</a></p> 
      } 
      else 
      { 
       var monitor = item as Monitor; 
       string man = @monitor.Manufacturer; 
       string mid = @monitor.ModelID; 
       string SN = @monitor.SerialNum; 
       int hr = @monitor.HoursON; 
       string TTi = @monitor.LastTestTime; 
       string TTy = @monitor.LastTestType; 

       <p>&ensp;&ensp;&ensp;&ensp;<a onclick="postItM('@man', '@SN','@hr','@mid','@TTi','@TTy')">@monitor.Manufacturer</a></p> 
      } 
     } 

</div> 

私はCompAndMonと呼ばれるメインビューに投稿しています。 2のための のコントローラは、このように見えるが、私は、私は「不慣れ」な方法

#Home Controller# 

     public ActionResult CompAndMon() 
    //I think I should put parameters in here but i am not sure 
    { 



     return View(); 
    } 
    public ActionResult _OCMList() 
    { 
     var ObjectList = new List<OBJECT>{ 
         new Office() {Type = 1,ID = 1, Name1 = "Fort Collins", OfficeName = "Fort Collins", OfficeNumber = "1", ContactNumber = "555-123-5555", ContactName = "ted" } , 
         new Computer() {Type = 2,ID = 2,Name1 = "Speed-Machine", Name = "Speed-Machine", LastUser = "Ted", NumMonitors = 1, FreeHDSpace = "12GB", NumUpdates = 0, TotalHDSpace = "50GB" } , 
         new Monitor() {Type = 3, ID = 3,Name1 = "Sony", Manufacturer = "Sony", HoursON = 20, LastTestTime = "11pm, August 31", LastTestType = "SMPTE", ModelID = "654123", SerialNum = "a36-f45-gh325"} , 
         new Office(){Type = 1,ID = 2, Name1 = "Denver", OfficeName = "Denver", OfficeNumber = "2", ContactNumber = "555-123-5555", ContactName = "Nick" } , 
         new Computer() {Type = 2, ID = 5,Name1 = "Nicks PC", Name = "Nicks PC", LastUser = "Ted", NumMonitors = 1, FreeHDSpace = "12GB", NumUpdates = 0, TotalHDSpace = "50GB" } , 
         new Monitor() {Type = 3, ID = 6,Name1 = "LG", Manufacturer = "LG", HoursON = 20, LastTestTime = "11pm, August 31", LastTestType = "SMPTE", ModelID = "654123", SerialNum = "a38-l87kp-g6j9"} , 
         new Computer() {Type = 2, ID = 7,Name1 = "Ted", Name = "FastOne", LastUser = "Ted", NumMonitors = 2, FreeHDSpace = "23GB", NumUpdates = 2, TotalHDSpace = "50GB" } , 
         new Monitor() {Type = 3, ID = 8,Name1 = "HTC", Manufacturer = "HTC", HoursON = 20, LastTestTime = "11pm, August 31", LastTestType = "SMPTE", ModelID = "654123", SerialNum = "d77-ko9-poo77" }, 
         new Monitor() {Type = 3, ID = 9,Name1 = "Panisonic", Manufacturer = "Panisonic",HoursON = 20, LastTestTime = "11pm, August 31", LastTestType = "SMPTE", ModelID = "654123", SerialNum = "h67-j567-lo99" } 
     }; 

     return PartialView(ObjectList); 
    } 

で情報を掲示するので、私のメインビューで重要であるどのようなパラメータを追加する必要がある場合はわからない

#CompAndMon 
///some javaScript or AJAX to grab posted values 
<div class="container" id="MyPartial"> 
       @Html.Action("_OCMList","Home") 

</div> 

私はMVCを初めて熟知しています。 質問をまとめる...メインビューに投稿された変数をどのように取得するのですか?

答えて

0

これを実現するには、PostModelパラメーターを使用し、GETと同じ名前の新しいアクションメソッドに値を送信します。属性を使用して2つを区別できます。

[HttpGet] 
    public ActionResult CompAndMon() 
    { 
     var viewModel = new ViewModel(); 
     return View(viewModel); 
    } 

    [HttpPost] 
    public ActionResult CompAndMon(PostModel model) 
    { 
     var viewModel = new ViewModel 
     { 
      Thing = model.Thing 
     }; 
     return View(viewModel) 
    } 

    public class PostModel 
    { 
     public object Thing { get; set; } 
    } 

    public class ViewModel 
    { 
     public object Thing {get; set; } 
    } 

次に、「メイン」ビューでこの値を確認して、値がポストバックされていればディスプレイに値を設定できます。

@model Project.Models.ViewModel 

<div class="container"> 
    @if (Model.Thing != null) 
    { 
     if (thing is Office office) 
     { 
       <!--Display Office--> 
     } 
     else if (thing is Computer computer) 
     { 
       <!--Display Office--> 
     } 
     else if (thing is Monitor monitor) 
     { 
       <!--Display Office--> 
     } 
    } 

    @Html.Action("_OCMList", "Home"); 
</div> 
+0

この作品は素晴らしいです!どうもありがとうございます! @jwhite –

関連する問題