0

I am using datatables.net and rendering a button. It works perfectly if I hand craft the element, but I want to use the razor helper class here. The first example below, returns an error about too many characters in character literal on the ' before + full.id.

The second, states full does not exist in the current context, I am not sure how to access the function parameter inside a C# helper? I need to use Page Model parameters and the specific value from the row the button is on.

I have spend hours doing various combinations, and can't get it to work.

"render": function (data, type, full, meta) {  return = '@Html.ActionLink("Create Breach", "SaleRedirect", "Admin", new  { SchemeId=Model.SchemeId, Currency=Model.Currency, AgencyRef=Model.AgencyRef, ClientNo=Model.ClientNo, BasePolicyTransactionNo = Model.PolicyTransactionNo, PolicyTransactionCoverageId=' + full.id + '}, new { area="", @class = "btn btn-primary" }) '

"render": function (data, type, full, meta) {return '@Html.ActionLink("Create Breach", "SaleRedirect", "Admin", new  { SchemeId=Model.SchemeId, Currency=Model.Currency, AgencyRef=Model.AgencyRef, ClientNo=Model.ClientNo, BasePolicyTransactionNo = Model.PolicyTransactionNo, PolicyTransactionCoverageId= full.id }, new { area="", @class = "btn btn-primary" }) '
1
  • I am not sure I get what you are trying to do, since the code would be executed an the server side. The full.id will always be simple text, which is why you are getting the character literal error. If you want to have a usable link for the client side, you should pre-generate your link server-side (if you insist on using Html.ActionLink with a placeholder that you then replace in JavaScript. Commented Oct 11, 2020 at 19:41

2 Answers 2

0
+100

Well, after writing my comment I think I have a rough idea about your issue. If I'm guessing correctly, the code you have shown is within a < script > tag on your razor page, if so, try the following:


   "render": function (data, type, full, meta) {  
   let linkWithPlaceholder = '@Html.ActionLink("Create Breach", "SaleRedirect", "Admin", new  { SchemeId=Model.SchemeId, Currency=Model.Currency, AgencyRef=Model.AgencyRef, ClientNo=Model.ClientNo, BasePolicyTransactionNo = Model.PolicyTransactionNo, PolicyTransactionCoverageId="{Placeholder}"}, new { area="", @class = "btn btn-primary" }'
linkWithPlaceholder = linkWithPlaceholder.replace({Placeholder}, full.id);
return linkWithPlaceholder;
}

Your current approach won't work, because the razor tag will be rendered before full.id has a value. Since the value is only present on the client side. Instead in your case, it is a C# char with + full.id +

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, the replace was not something I was aware of.
0

Please have a look at the answer in the link below. Hope it will help you achieve want you want to.

HTML Helper in JavaScript?

1 Comment

Not really @Saifiqbal, I have read similar which got me to spend the hours trying every combination going (but clearly not the right one), the concept is clearly fine, what it states makes sense and the scenario it doesn't cover is that I am inject one inside the others output. If it was simply a concatenation I think it would be easy.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.