2017-09-01 66 views
-1

控制器:從控制器獲取數據後,以查看,如何將數據發送回視圖

{ 
    private static string selection = String.Empty; 
    dynamic mymodel = new ExpandoObject(); 
    public ActionResult Post(string Name) 
    { 
     selection = Name; 
     return RedirectToAction("Index"); 
    } 
    public ActionResult Index() 
    { 
     SegmentRepository segment = new SegmentRepository(); 
     mymodel.listofSegments = segment.GetSegmentation(); 
     DynamicRepository dynamic = new DynamicRepository(); 
     mymodel.listofDynamic = dynamic.GetDynamicContent(selection); //After selecting the segmentation in the view it returns the required dynamic content in mymodel.listofDynamic but does not display it in the view. 
     return View(mymodel); 
    } 
} 

在視圖中選擇的分割後,它返回所需要的動態內容在mymodel.listofDynamic但確實不要在視圖中顯示它。

查看:

<script> 
    function seg() { 
     var employment = document.getElementById("Employment").value; 
     $.ajax({ 
      type: "POST", //HTTP POST Method 
      url: '@Url.Action("Post","Home")', // Controller/View 
      data: { 
       //Passing data 
       Name: employment //Reading text box values using Jquery 
      } 
     }) 
    } 
</script> 

<tr> 
    <td height="100"> 
     <label>220</label> 
    </td> 
    <td> 
     <select id="Employment"> 
      <option>---Select---</option> 
      @foreach (var item in Model.listofSegments) 
      {  
       <option name="selectedSegment" value="@item">@item</option> 
      } 
     </select> 
     <input type="submit" value="Send" name="submit" onclick="seg()"> 
    </td> 
    <td> 
     <select name="Dynamic"> 
      <option>---Select---</option> 
      @foreach (var item in Model.listofDynamic) 
      { 
       <option name="selectedDynamic" value="@item">@item</option> 
      }// I need the data to get listed here 
     </select> 
     <input type="submit" value="Send" name="Submit1"> 
    </td> 
</tr> 

我需要的public ActionResult Index()方法,以便在listofDynamic數據視圖獲取打印再次運行。我怎樣才能做到這一點?

+0

你做了一個ajax調用 - ajax調用永遠不會重定向(它們的全部重點是保持在同一頁面上)。做一個正常的提交到你的'Post()'方法 –

回答

0

Ajax的數據部分中,使用以下:

data: { "name": employment }, //Pass value of employment variable as the name parameter 
success: function(data) { 
    alert(data); //In the success function, pass the data to be notified in the view 
} 

上面將從控制器返回數據使用Ajax查看。

在同一時間,使一個Json方法,使工作或ActionResult也會做:

public JsonResult GetResults(string name) //Here is the name parameter 
{ 
    MainEntities db = new MainEntities(); //Db context 

    var con = (from c in db.TableName 
       where c.ColumnName == name //Pass the name parameter in the query 
       select c).ToList(); 

    return Json(con); //Finally returns the result in Json 
} 
0

如果你是想通過Ajax的,你忘了成功和錯誤部分得到的值。 查看部分:

$.ajax({ 
      type: "POST", //HTTP POST Method 
      url: '@Url.Action("Post","Home")', // Controller/View 
      data: { 
       //Passing data 
       Name: employment //Reading text box values using Jquery 
      }, 
      success:function(result){alert(result);}, 
      error:function(error){alert(error.responseText);} 
     }) 

控制器側像AT-2017寫道JsonResult或者你可以返回局部視圖/任何其他類型。

相關問題