2017-06-12 81 views
-1

我想在主頁類型視圖上預覽一個視圖。爲此,我想調用ListPreviews操作。我想要這個動作來獲得給定視圖的html正文,然後取第一百個字符左右。從控制器獲取視圖的HTML - .NET MVC

如何從控制器訪問實際的視圖html?

+0

能否請你讓我知道我可以如何進一步幫助你? – kblau

回答

0

這應該很簡單。 在你RouteConfig.cs設置的默認值,礦山看起來像這樣:

defaults: new { controller = "Home", action = "Index2006", id = UrlParameter.Optional } 

爲控制器/型號:

public class AView 
{ 
    public string theHtml { get; set; } 
} 

public class HomeController : Controller 
{ 
    [HttpPost] 
    public ActionResult Index2005(AView AView) 
    { 
     //put breakpoint here to see all the <html> here in view 
     var result = HttpUtility.UrlDecode(AView.theHtml, System.Text.Encoding.Default); 
     return Json(new 
     { 
      Greeting = "Returning data not used" 
     } 
     , @"application/json"); 
    } 

對於您的觀點:

<!DOCTYPE html> 
<html id="PassMe"> 
<head> 
    <meta name="viewport" content="width=device-width" /> 
    <title>Index2005</title> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> 
    <script type="text/javascript"> 
     $(function() { 
      $(".btn").click(function() { 
       var AView = { theHtml: escape($("#PassMe").html()) }; //JSON.stringify($("#PassMe").html()) 
       $.ajax({ 
        url: '/Home/Index2005', 
        type: 'POST', 
        data: AView, 
        success: function (result) { 
         $("#detail").append(result.Greeting); 
        }, 
        error: function (result) { 
         alert('Error'); 
        } 
       }); 
      }); 
     }); 
    </script> 
</head> 
<body> 
    <button style="margin-bottom: 20px;" class="btn btn-default">Click to pass HTML</button> 
</body> 
</html> 
+0

@bchattdeveloper你可以讓我知道我可以怎樣幫助你嗎? – kblau

相關問題