0

In my visual studio 2015 mvc web application, I need to create (append) the successful Ajax return data and append them to a webgrid, but the following code don't compile, the code of dataval (id = dataval.id) within the var html can not be recognized (compiled).

Does anybody know how to make it working? basically, I need to create those Html.ActionLink with other data returned from ajax call.

$.each(response, function (j, dataval) {
  var html='@Html.Raw(HttpUtility.HtmlDecode(Html.ActionLink("Edit", "Edit", new { id = dataval.id }).ToHtmlString()))';
  html+='@Html.Raw(HttpUtility.HtmlDecode(Html.ActionLink("create", "Create", new { id = dataval.id }).ToHtmlString()))';
  html+='@Html.Raw(HttpUtility.HtmlDecode(Html.ActionLink("Invoice", "Invoice", new { id = dataval.id }).ToHtmlString()))';
  html+='@Html.Raw(HttpUtility.HtmlDecode(Html.ActionLink("Delete", "Delete", new { id = dataval.id }).ToHtmlString()))';

  $("#GridProductIndex").append('<tr><td>' + dataval.Prod_Name + '</td> + <td>' + dataval.Prod_Type + '</td> +<td>' + dataval.Prod_Status + '</td><td>' +
    html  + '</td></tr>'); 

});

Added more, here is the webgrid I want to append the above data to:

grid.Columns(
  grid.Column(columnName: "Prod_Name", header: "Product Name"),
  grid.Column(columnName: "Prod_Type", header: "Type"),
  grid.Column(columnName: "Prod_Status", header: "Status"),
  grid.Column(header: "Actions", format: (item) =>
    new HtmlString(

      Html.ActionLink("Edit", "Edit", new { id = item.ID }).ToString() + " | " +

      Html.ActionLink("create", "Create", new { id = item.ID }).ToString() + " | " +
      Html.ActionLink("Invoice", "Invoice", new { id = item.ID }).ToString() + " | " +
      Html.ActionLink("Delete", "Delete", new { id = item.ID }).ToString()

    ), style: "Action"

  )
green
  • 25
  • 6

2 Answers2

0

it's probably a Problem that you use single quotes inside a string that is enclsoed in single quotes => use double quotes in the .append(...) line

iPirat
  • 2,197
  • 1
  • 17
  • 30
  • it is not the dataval in append, it is the dataval in the var html = xxx, like this: var html='@Html.Raw(HttpUtility.HtmlDecode(Html.ActionLink("Edit", "Edit", new { id = dataval.id }).ToHtmlString()))'; // the 'dataval' is not compiled in the 4 lines of these. – green Aug 18 '18 at 12:21
  • Sorry, I thought you have unescaped single quotes inside a single-quoted-string (`'$each... '@Html...' ... '' ... );'` Did you check that: https://stackoverflow.com/questions/200476/html-actionlink-method ? – iPirat Aug 18 '18 at 12:46
  • I also tried this: var t1 = dataval.id; //here it OK, the "datival" is compiled properly,but the "t1" still can not be recognized below var html = '@Html.Raw(HttpUtility.HtmlDecode(Html.ActionLink("Edit", "Edit", new { id = t1 }).ToHtmlString()))'; //"t1" doesn't compile here. – green Aug 18 '18 at 12:49
  • note that `dataval` is a n argument of an Javascript function. `new { id =..}` is ASP code – iPirat Aug 18 '18 at 12:56
  • Hi iPirat, I have just checked that post, but I don't think it is related to my question, this is not about how to create Html.ActionLink or how to pass parameters through ActionLink, this is about how to convert the existing Html.ActionLink from Ajax successful returned data to native html and append it as a row of table data to a webgrid(html table) – green Aug 18 '18 at 12:56
  • No sure if this will be working, but I will try it tomorrow and post the result then. var html='@Html.Raw(HttpUtility.HtmlDecode(Html.ActionLink("Edit", "Edit", new { id =' + datival + ' xxx – green Aug 19 '18 at 12:14
0

After some investigations and searching, here is the solution:

var html = '<a href="@Url.Action("Edit")?id='+ dataval.id + '">Edit</a>';

The solution is found from here: https://forums.asp.net/t/1977920.aspx?Create+ActionLink+by+jQuery

green
  • 25
  • 6