简体   繁体   中英

How do I rewrite this into MVC Razor 3?

Its a simple snippet, but the .replace is not being recognize in my mvc razor view: I replaced <%= %> with @ , not sure what else is needed?

 <%=Ajax.ActionLink("[replacethis]", 
        "ToggleEnabled", 
                new { id = Model.ID }, 
                new AjaxOptions { UpdateTargetId = "toggleimage" + Model.ID }).Replace("[replacethis]",
                string.Format("<div id='toggleimage{0}'><img src='/Content/icons/{1}' border='0' alt='toggle'/></div>", 
                Model.ID, Model.Enabled ? "tick.png" : "tick_grey.png"))%>

I'm not sure if this is the ideal, but you can get what you're after by:

  1. Inserting .ToHtmlString() (ie, convert MvcHtmlString to a raw HTML string so that you can call Replace on it)
  2. Wrapping the whole thing in Html.Raw() (so that it doesn't get HTML encoded before rendering)

@Html.Raw(
    Ajax.ActionLink("[replacethis]", 
        "ToggleEnabled", 
        new { id = Model.ID }, 
        new AjaxOptions { UpdateTargetId = "toggleimage" + Model.ID })
   .ToHtmlString()
   .Replace("[replacethis]",
       string.Format("<div id='toggleimage{0}'><img src='/Content/icons/{1}' border='0' alt='toggle'/></div>", 
           Model.ID, Model.Enabled ? "tick.png" : "tick_grey.png")
   )
)

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