3

Is there any way to put html.helper inside a .append() function.

success: function (html) { $("#Resultaatsgebied").append(' <li>@Html.TextBoxFor(item => Model.RG1)<ul id="Concretisering" class="subList"><li>@Html.TextBoxFor(item => Model.RG1sub1)</li></ul>@Html.ActionLink("+", "index", null, new { id = "addConcretisering" })</li>'); }

This works, but just as a string, not as a control

EDIT:

This works now, but the actionlink who is generated does not. I have the same actionlink default in my view, and there it works. But for the generated one, I think he just does not execute the ajax call?

3
  • 1
    it is bceause this is executed in the client side, not in the server Commented Apr 18, 2013 at 7:07
  • so it can't be fixed? Commented Apr 18, 2013 at 7:09
  • I don't know about asp.net, but i think it has a fragment concept where you can create a html fragment and return it to client Commented Apr 18, 2013 at 7:16

4 Answers 4

1

If your code is in a razor view, you can put into brackets your razor code like this :

success: function (html) { 
    $("#Resultaatsgebied").append(' <li>@(Html.TextBoxFor(item => Model.RG1))<ul id="Concretisering" class="subList"><li>@(Html.TextBoxFor(item => Model.RG1sub1))</li></ul>@(Html.ActionLink("+", "index", null, new { id = "addConcretisering" }))</li>'); 
}
Sign up to request clarification or add additional context in comments.

2 Comments

did this work for anyone?? doesn't work for me on MVC 5 razor (cshtml)
I tried it with MVC 5 and does not seems to be working.
0

I think you can not do that in pure Javascript. Why don't you set the returned data from AJAX as HTML and you can set it for any div you want. For example in your case:

$("#Resultaatsgebied").html(html);

Comments

0

May be a better concatenation like this (not sure but you can try):

success: function (html) { 
    $("#Resultaatsgebied").append(
          '<li>' + @Html.TextBoxFor(item => Model.RG1) + 
          '<ul id="Concretisering" class="subList"><li>' + 
             @Html.TextBoxFor(item => Model.RG1sub1) + 
          '</li></ul>' + 
             @Html.ActionLink("+", "index", null, new { id = "addConcretisering" }) + 
          '</li>'); 
}

Comments

0

In MVC 5 razor or newest versions, This quota --> ' not working anymore. Therefore, use ( ` ) mark to do your work

success: function (html) {     
   $("#Resultaatsgebied").append(` 
      <li>@Html.TextBoxFor(item => Model.RG1)    
           <ul id="Concretisering" class="subList">     
                <li>@Html.TextBoxFor(item => Model.RG1sub1)</li>   
           </ul>      
           @Html.ActionLink("+", "index", null, new { id = "addConcretisering" })    
      </li>
  `);    
}

Comments

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.