Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6089569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:06:51+00:00 2026-05-23T12:06:51+00:00

I have a view with adding element to collection: It adds me element to

  • 0

I have a view with adding element to collection:
It adds me element to html, but when I call ActionResult the Model.TagModelsis still empty(at start is empty).

<div id="tags_div">
            <label id="add_tag">Hello</label>
            @foreach (var item in Model.TagModels)
            { 

                <div class="editor-field">
                    @Html.EditorFor(model => item.Name)
                    @Html.ValidationMessageFor(model => item.Name)
                </div>

            }</div>

        <script type="text/javascript">
            $(document).ready(function () {
                var $newdiv1 = $('<div class="ui-button-text"><input class="text-box single-line" id="item_Name" name="item.Name" type="text" value="b" /><span class="field-validation-valid" data-valmsg-for="item.Name" data-valmsg-replace="true"></span></div>');
                $("#add_tag").live("click", function () {
                    $(this).append($newdiv1);
                    return false;
                });
            });
        </script>

Here is my viewmodel:

public class QuestionTagViewModel
    {
        public QuestionModel QuestionModel { get; set; }

        //private List<TagModel> _tagModels=new List<TagModel>(){new TagModel(){Name = "a"},new TagModel(){Name = "b"}};
        private List<TagModel> _tagModels = new List<TagModel>();

        public List<TagModel> TagModels
        {
            get { return _tagModels; }
            set { _tagModels = value; }
        }
    }

Why there is no update on model?
When I change others properties for static elements everything is ok

EDIT
To MHollis:

Thanks Anton for your reply 🙂

Now I have

<script type="text/javascript">
            $(document).ready(function () {
                $("#addItem").click(function () {
                    $.ajax({
                        url: this.href,
                        cache: false,
                        success: function (html) { $("#tags_div").append(html); }
                    });
                    return false;
                });
            });
        </script>

And tags_div:

<div id="tags_div">
            <label id="add_tag">Hello</label>
            @Html.ActionLink("Add another...", "BlankEditorRow", null, new { id = "addItem" })

            @foreach (var item in Model.TagModels)
            { 

                <div class="editor-field">
                    @Html.EditorFor(model => item.Name)
                </div>

            }</div>

And that the PartialViewResult method:

public PartialViewResult BlankEditorRow()
        {
            var x=PartialView("TagRow", new TagModel());
            return x;
        }

and partial view:

@*@using Szamam.Models
@model TagModel

<div class="editor-field">
    @Html.EditorFor(model => model.Name)
    @Html.ValidationMessageFor(model => model.Name)
</div>*@


<div class="editor-field">
<input class="text-box single-line" id="[0].Name"  name="[0].Name" type="text" value="b" />
</div>

Element is adding but when fie actionresult, collection is empty :/

So there should be another reason:/

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T12:06:52+00:00Added an answer on May 23, 2026 at 12:06 pm

    It’s because the inputs you’re adding through javascript have the id of “item_name” and name of “item.name”. They would need to have the name of “TagModels[0].Name”, “TagModels[1].Name”, “TagModel[2].Name”.
    Note: This is going off information found here

    UPDATE

    Ok, so I went ahead and took the time to make this work given the data you provided. Thankfully I am needing something similar for a project I’m working on right now, unfortunately my project is in VB.Net, but to help you I did this code in C# (obviously).
    Note: This code you could copy and paste into a clean project, except for the lack of using statements.

    View

    @model MvcApplication1.Models.QuestionTagViewModel
    @{
        Layout = null;
    }
    <!DOCTYPE html>
    <html>
    <head>
        <script src="../../Scripts/jquery-1.5.1.js" type="text/javascript"></script>
        <script src="../../Scripts/jquery-ui-1.8.11.js" type="text/javascript"></script>
        <script src="../../Scripts/jquery.unobtrusive-ajax.js" type="text/javascript"></script>
        <script src="../../Scripts/jquery.validate.js" type="text/javascript"></script>
        <script src="../../Scripts/jquery.validate.unobtrusive.js" type="text/javascript"></script>
        <script type="text/javascript">
            $(document).ready(function() {
                var re = /\[(.*?)\]/ ;
                $("#add_tag").click(function() {
                    var name = $(".tagRows:last>input").attr("name");
                    var m = re.exec(name);
                    var itemNumber = parseInt(m[1]) + 1;
                    var $newdiv1 = $('<div class="tagRows"><input class="text-box single-line" id="TagModels' + itemNumber + '__Name" name="TagModels[' + itemNumber + '].Name" type="text"><span class="field-validation-valid" data-valmsg-for="TagModels[' + itemNumber + '].Name" data-valmsg-replace="true"></span></div>');
                    $(".tagRows:last").after($newdiv1);
                    return false;
                });
            });
        </script>
        <title>TagsTest</title>
    </head>
    <body>
        @using (Html.BeginForm())
        { 
            <div id="tags_div">
                <label id="add_tag">
                    Hello</label>
                @for (int i = 0; i < Model.TagModels.Count; i++)
                {
                    <div class="tagRows">
                        @Html.EditorFor(model => model.TagModels[i].Name)
                        @Html.ValidationMessageFor(model => model.TagModels[i].Name)
                    </div>
                }
            </div>
            <input type="submit" name="submit" value="Submit" />
        }
    </body>
    </html>
    

    Model

    namespace MvcApplication1.Models
    {
        public class QuestionTagViewModel
        {
    
            //private List<TagModel> _tagModels=new List<TagModel>(){new TagModel(){Name = "a"},new TagModel(){Name = "b"}};
            private List<TagModel> _tagModels = new List<TagModel>();
    
            public List<TagModel> TagModels
            {
                get { return _tagModels; }
                set { _tagModels = value; }
            }
        }
    
        public class TagModel
        {
            public string Name { get; set; }
        }
    }
    

    Controller

    namespace MvcApplication1.Controllers
    {
        public class HomeController : Controller
        {
    
            public ActionResult TagsTest()
            {
                var tagmodels = new List<TagModel>();
                tagmodels.Add(new TagModel() { Name = "Tag1" });
                tagmodels.Add(new TagModel() { Name = "Tag2" });
                tagmodels.Add(new TagModel() { Name = "Tag3" });
                var model = new QuestionTagViewModel() { TagModels = tagmodels };
    
    
                return View(model);
            }
    
            [HttpPost]
            public ActionResult TagsTest(QuestionTagViewModel model)
            {
                return null;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a UIView element which I am adding to the main view controller.
I have a table view that I am customizing and I am adding a
I have a root view controller with no nib file, I tried adding this
i have view like 'home/details/5', it can be access by anonymous user. but there
We have a View (call it X) that is the base view called by
I'm having difficultly adding querystring parameters to link_to UrlHelper. I have an Index view,
I have a DOM element similar to Facebook's View friends popup. I have a
I have view controller, into the view i have put a table view, and
I have a view that has a list of jobs in it, with data
I have a view using a master page that contains some javascript that needs

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.