簡體   English   中英

剃刀視圖錯誤MVC ASP.NET

[英]Razor view error mvc asp.net

我對mvc應用程序處於初級階段,所以請尋求幫助。

我試圖embed c#jquery測試如果user.identity是在認證的條件document.load ,但我發現cs1001 (標識符預期)。

 $(document).load(function () { if (@{User.Identity.Name != "Someone"}){ alert("Something"); } }); 

從if刪除花括號

if (@User.Identity.Name != "Someone"){
    alert("Something");
}

您可以執行以下操作:

@if (User.Identity.Name != "Someone"){
   @:alert("Something");
}

要么

@if (User.Identity.Name != "Someone"){
   <text>alert("Something");</text>
}

要么

var identityName = @Html.Raw(Json.Encode(User.Identity.Name));

if (identityName  != "Someone"}){
    alert("Something");
}

要么

if ("@User.Identity.Name" != "Someone"){
    alert("Something");
}

前兩個是C# if條件,並且是在服務器端計算的。 而最后2個是javascript if塊,它將在客戶端運行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM