Dynamically add item to collection
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>
}</di开发者_JS百科v>
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 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;
}
}
}
精彩评论