2012-07-08 62 views
0

我有一個簡單的腳本的視圖,例如:MVC操作方法和Javascript

<script type="txt/javascript"> 
    function DoSomething(param) 
    { 
    alert(param); 
    } 
</script> 

反正是有,我可以調用該函數在一個控制器的動作方法的返回結果的價值?

編輯:

我真的很希望創造/註冊的JavaScript類與實體框架值。 如果無法完成,我如何創建/註冊具有EntityFramework值的Javascript類?

OK,這裏的東西: 我已經有一個JS的功能是這樣的:

RegisterClasses(param1, param2) 
    { 
    ...... 
    } 

我已經看到了在其他地方直通提琴手做,但不知道他們是如何做到這一點。

如何將值傳遞給該JS函數以從EF中的值創建類?

+0

在現實中,我嘗試註冊/創建JavaScript類與EF類,但不想的問題 – 2012-07-08 02:08:39

回答

1

你可以做的就是把它添加到視圖:

<script type="text/javascript">DoSomething(@Model.Param)</script> 

添加Param財產的行動視圖模型。

對於使用EF類,你可以這樣做:

<script type="text/javascript"> 
    var jsModel = { 
        name: "@Model.Name", 
        age: @Model.Age 
        }; 
</script> 

而且,喲可以將任何物體只是轉換成JSON以下這個答案:Convert .Net object to JSON object in the view

希望它能幫助。

1

你可以JSON序列模型的觀點:

@model MyViewModel 

<script type="text/javascript"> 
    var model = @Html.Raw(Json.Encode(Model)); 

    // now the model variable is a JSON representation of your 
    // server side model that was passed by the controller action 
    // to this view and you could manipulate it as a standard js variable 


    // So assuming your model contains 2 properties Param1 and Param2 you 
    // could pass them to your javascript method 
    RegisterClasses(model.Param1, model.Param2); 

</script> 
+0

我不知道'複雜Json.Encode()'。好的! – ivowiblo 2012-07-10 01:38:09