0

I have this line but not sure about the + string part. It is not correct as it is now:

 <p>
    <%: Html.ActionLink("TUK-JS-KPI-WE-" + ((DateTime)Eval(ViewBag.jobSortedReportDate)).ToString("yyyy-MM-dd"), "TradeUKKPIReportInstructions", "Report", new { @date = ViewBag.jobSortedReportDate }, 0)%>    
 </p>

How do I add text to value?

tereško
  • 58,060
  • 25
  • 98
  • 150
charlie_cat
  • 1,830
  • 7
  • 44
  • 73

2 Answers2

3

If jobSortedReportDate contains string value you should cast it to a string, just like like that:

<%: Html.ActionLink("TUK-JS-KPI-WE-" + (string)ViewBag.jobSortedReportDate, "TradeUKKPIReportInstructions", "Report", new { @date = ViewBag.jobSortedReportDate }, 0)%> 
krolik
  • 5,712
  • 1
  • 26
  • 30
  • yup thank you, just found that out, but got an error yet again, cannot convert type DateTime to string..let me see quick :) – charlie_cat Aug 21 '12 at 07:25
  • In this case cast it to a DateTime instead of string `(DateTime)ViewBag.jobSortedReportDate` – krolik Aug 21 '12 at 07:28
  • Yikes yes correct, sorry a bit slow today, was up late studying :) I want to take this one step further and have this: <%: Html.ActionLink("TUK-JS-KPI-WE-" + (DateTime)ViewBag.jobSortedReportDate.ToString("yyyy-MM-dd"),...) %> but obviously this is not correct – charlie_cat Aug 21 '12 at 08:18
  • You should definitely read more about casting and type convertions ;) `((DateTime)ViewBag.jobSortedReportDate).ToString("yyyy-MM-dd")` – krolik Aug 21 '12 at 08:50
0

You can also try this

<%: Html.ActionLink(String.Format("TUK-JS-KPI-WE-{0}", (DateTime) ViewBag.jobSortedReportDate), "TradeUKKPIReportInstructions", "Report", new { @date = ViewBag.jobSortedReportDate }, 0)%>

Here is alternative

<a href="@Url.Action("TradeUKKPIReportInstructions", new {Controller = "Report", @date = ViewBag.jobSortedReportDate })">@String.Format("TUK-JS-KPI-WE-{0}", ViewBag.jobSortedReportDate )</a>
codingbiz
  • 26,179
  • 8
  • 59
  • 96
  • oops I forgot about the System.Format, but still get error: 'System.Web.Mvc.HtmlHelper' has no applicable method named 'ActionLink' but appears to have an extension method by that name. Extension methods cannot be dynamically dispatched. Consider casting the dynamic arguments or calling the extension method without the extension method syntax. I must just add: My line is in the view.aspx – charlie_cat Aug 21 '12 at 07:45
  • You need to cast to DateTime in the String.Format to make it work with the ViewBag `String.Format("TUK-JS-KPI-WE-{0}", (DateTime)ViewBag.jobSortedReportDate)` – nemesv Aug 21 '12 at 08:07
  • Thanks. +1 to you. I used it with `ViewBag.Title` thinking that is a string. Now I know better – codingbiz Aug 21 '12 at 08:10