2011-04-16 39 views

回答

0

你可以讓你的類型編寫腳本如下:

[ScriptableType] 
public partial class MainPage : UserControl 
{ 

    public MainPage() 
    { 
     InitializeComponent(); 

     HtmlPage.RegisterScriptableObject("MainPage", this); 
    } 

    [ScriptableMember] 
    public string Text 
    { 
     get { return TextBox1.Text; } 
     set { TextBox1.Text = value; } 
    } 

} 

現在你有機會獲得MainPage.Text財產。

<script type="text/javascript"> 
    Silverlight.createObjectEx({ 
     source: '/clientbin/journal.web.administration.xap', 
     parentElement: document.getElementById('SilverlightPlaceHolder'), 
     id: 'SilverlightPlugin', 
     properties: { 
      version: '4.0.60129.0' 
     }, 
     events: { 
      onLoad: function() { 
       var host = document.getElementById('SilverlightPlugin'); 
       if (host != null) 
        host.content.MainPage.set_Text('@ViewBag.Text'); 
      } 
     } 
    }); 
</script> 

最後,來自行動返回的字符串是這樣的:

public ActionResult Index() 
{ 
    ViewBag.Text = "Some Text"; 

    return View("Index"); 
} 

更新:如果您正在使用ASP.NET MVC 2,您必須將ViewBag.Text改變計算機[ 「文字」]並在標記中將@ViewBag.Text更改爲<%: ViewData["Text"] %>

+0

謝謝。我可以通過設置initparm值來實現這一點,但這似乎是一個更好的方法。 – Sanjay 2011-04-16 06:22:36