2017-08-29 75 views
0

控制器:如何從控制器的數據,查看刷新

public class HomeController : Controller 
{ 
    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所需的動態內容,但在視圖中不顯示它在視圖中選擇分割後。

查看:

//Ajax 
<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 
     } 
    } 
    ) 
} 

<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"> 
      </tr> 

極品的ActionResult指數因此在listofDynamic的數據視圖獲取打印再次運行。

回答

0

嘗試重新加載你需要的div'成功'方法的Ajax。在這裏,您將返回響應(純HTML作爲視圖在這裏),而不是綁定到任何地方。

ajax調用是異步的,它不會重新加載整個視圖。

查找下面

$.ajax( 


     { 
      type: "POST", 
      url: '@Url.Action("Index", "Home")', 
      success: function (returnData,success) 
      { 

       $("#sampleDiv").html(returnData); 

      } 

     } 
    ); 
代碼
相關問題