我想將整個Model對象與另一個參數傳遞給js函數launchMyActivity()。但功能沒有得到它。當我做了aler(passedObject.length)
它給不明身份。無法將Model對象傳遞給javascript函數
我試過其他的東西,但沒有任何工作。 Referred this but didnt worked
我不確定我的實現出了什麼問題。
型號:
public class RequestDTO
{
public HttpRequestBase RequestDto { get; set; }
public List<User> Users { get; set; }
public string ActionId { get; set; }
public string UserSampleID { get; set; }
}
查看:
@model RequestDTO
<body>
@Scripts.Render("~/bundle/js/Area/SelectedStudentActivity")
<div class='popOver' style="z-index: 1012;">
<div id='popOverMsg'>Select the student you want to Launch this Activity for:..</div><br /><br />
@foreach(var user in Model.Users)
{
<a id="selectedAnchor" data-id="@user.id" style="cursor:pointer" onclick="launchMyActivity(@user.UserID,@Model)">@user.name</a><br />
}
</div>
</body>
JS:
function launchMyActivity(studentSISId, requestDTO)
{
alert("Lenght : " + requestDTO.length); //I get undefined here
//Below this I need to make AJAX call and pass this requestDTO object.
}
模型是一個C#對象,但您試圖將其傳遞到JavaScript。您需要對其進行jsonify,以便javascript瞭解它。 – nurdyguy
試試這個:http://stackoverflow.com/questions/6201529/turn-c-sharp-object-into-a-json-string-in-net-4 – nurdyguy
你在使用CSHTML嗎?試試這個函數launchMyActivity(studentSISId) alert(「Lenght:」+ @ Model.length); } – Shan