c#
  • asp.net
  • asp.net-mvc
  • razor
  • 2017-07-31 159 views 2 likes 
    2

    我正在用C#和.NET Framework 4.7開發ASP.NET MVC 5應用程序。將Model屬性作爲參數傳遞給Url.Action

    我遇到了麻煩,試圖通過Url.Action傳遞參數。當我使用這個:

    <input type="button" value="@Resources.ProductCreateOmitCaption" 
        onclick='location.href=<%: Url.Action("Index", "ProductionOrder", new { isWizard = @Model.IsWizard}) %>' /> 
    

    我得到這個:

    <input type="button" value="Continue" 
        onclick='location.href=<%: Url.Action("Index", "ProductionOrder", new { isWizard =}) %>' /> 
    

    我也曾嘗試:

    <input type="button" value="@Resources.ProductCreateOmitCaption" 
        onclick="location.href='<%: Url.Action("Index", "ProductionOrder", new { isWizard = @Model.IsWizard}) %>'" /> 
    

    這帶來的另一個結果是:

    <input type="button" value="Continue" 
        onclick="location.href='<%: Url.Action("Index", "ProductionOrder", new { isWizard = False}) %>'" /> 
    

    與錯誤:

    JavaScript critical error at line 177, column 60 in http://AnotherPC:53827/Products/Create \n\nSCRIPT1015: Unfinished string constant

    我該怎麼辦?

    順便說一下,我使用剃刀。

    +0

    您使用的ASPX視圖或Razor視圖引擎? –

    +0

    您不能像aspx代碼那樣使用剃鬚刀 - 使用@ @ Url.Action(「Index」,「ProductionOrder」,新{isWizard = @ Model.IsWizard}) –

    +0

    只需刪除<%: and %> – daniell89

    回答

    3

    標記和@Resources指出您正在使用Razor視圖引擎的事實。然而,你的網址是使用aspx的。不應該混合使用這兩種,所以如果你使用Razor,堅持Razor - aspx標記根本不會被識別。因此:

    <input type="button" value="@Resources.ProductCreateOmitCaption" 
        onclick="location.href='@Url.Action("Index", "ProductionOrder", new { isWizard = Model.IsWizard})'" /> 
    
    +0

    完美答案! –

    0

    試試這個

    <input type="button" value="@Resources.ProductCreateOmitCaption" 
        onclick='location.href = @Url.Action("Index","ProductionOrder")' 
         + '?isWizard='[email protected] +'/>' 
    
    +0

    OP正在使用剃鬚刀視圖。 –

    +0

    @PowerStar不知道 –

    +0

    好吧:) cool !!! –

    相關問題