简体   繁体   中英

How to add code logic inside @Url.Action()?

location.href = '@Url.Action("PageIndex","Home",new {id = ViewBag.swfLoc })';

Above code snippet is resides in the <script> tag of my razor View *.cshtml file.

What I ask is : Can I add some code logic in side Url.Action function parameter.

eg

 location.href = '@Url.Action("PageIndex","Home",new {id = ViewBag.swfLoc.isEmpty() ? "other" : "this" })';

Other than declare a variable inside top razor block @{} because I don't want to declare many dummy variables.

If possible, how? Use string of C# or JS string for this?

Thanks in advance!

是的,它起作用了,您只需要检查null值

location.href = '@Url.Action("PageIndex", "Home", new { id = (ViewBag.swfLoc == null || ViewBag.swfLoc.isEmpty()) ? "other" : "this" })';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM