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:/
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
Controller