2

I have this Model and return its when form Edit :

public class NewsVM
{
    public List<string> Tags{get;set;}
}

and in Action:

public ActionResult Edit()
{
     //create instance and fill it
     return View(mymodel);
}

and in jQuery in View I want Get Tags and Add All of them to my div . How can I do this ?

I use this but not work :

@if (Model != null)
{
    <script type="text/javascript">

        for (var i = 0; i < @Model.Tags.Count; i++) {
            alert('@Model.Tags["i"]');
        }

     </script>
}
2
  • Why don't you just use razor to add them? Commented Oct 17, 2015 at 9:05
  • can You your answer ? I want Get All List<stting> and Add All of them to the div . Commented Oct 17, 2015 at 9:06

2 Answers 2

7

You need to convert the model to a javascript array

var tags = @Html.Raw(Json.Encode(Model.Tags));
for (var i = 0; i < tags.length; i++) {
    alert(tags[i]);
}
Sign up to request clarification or add additional context in comments.

Comments

0
@if (Model != null)
{
         for (var i = 0; i < Model.Tags.Count; i++) 
         {
             <script type="text/javascript">
                  alert('@Model.Tags[i]');
             </script>
         }
}

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.