c# - Editing a variable length list. View Model IEnumerable<> property is comming null from post method -
i need edit variable length list, i've followed steven sanderson's blog , ivan zlatev's blog , comes this. model:
public class tagviewmodel { [required] [stringlength(200)] [datatype(datatype.text)] [display(name = "Тип")] public string name { get; set; } [required] [stringlength(200)] [datatype(datatype.text)] [display(name = "Значение")] public string value { get; set; } [hiddeninput(displayvalue = false)] public long id { get; set; } [hiddeninput(displayvalue = false)] public long owner { get; set; } [hiddeninput(displayvalue = false)] public spectreenodetype ownertype { get; set; } }
view:
@model ienumerable<security.models.tagviewmodel> @{ viewbag.title = "tag editor"; } <h2>tag editor</h2> @using(html.beginform("edittags", "treeview", formmethod.post)) { <div id="editorrows"> @for (int = 0; < model.count(); i++) { html.renderpartial("_partialtageditor", model.elementat(i)); } </div> @html.actionlink("add tag", "addtag", null, new { id = "additem" }); <input type="submit" value="save changes" /> } <script> $("#editorrows").on("click", "a.deleterow", function () { $(this).closest("div.editorrow").remove(); }); $("#additem").click(function () { $.ajax({ url: this.href, cache: false, success: function (html) { $("#editorrows").append(html); } }); return false; }); </script>
partial view:
@model security.models.tagviewmodel <div class="editorrow"> @using (html.begincollectionitem("tagviewmodel")) { @html.labelfor(x => x.name) @html.textboxfor(x => x.name) @html.labelfor(x => x.value) @html.textboxfor(x => x.value) @html.editorfor(x => x.id) @html.editorfor(x => x.owner) @html.editorfor(x => x.ownertype) <a href="#" class="deleterow">delete</a> } </div>
post action method:
[httppost] public actionresult edittags(ienumerable<tagviewmodel> taglist) { return view("index"); }
fiddler shows, edittags post request contains required data:
tagviewmodel.index c1ec8d7c-cfc9-4cdc-b32c-6446d4bb476c tagviewmodel[c1ec8d7c-cfc9-4cdc-b32c-6446d4bb476c].name length tagviewmodel[c1ec8d7c-cfc9-4cdc-b32c-6446d4bb476c].value 100 - 200 tagviewmodel[c1ec8d7c-cfc9-4cdc-b32c-6446d4bb476c].id 1 tagviewmodel[c1ec8d7c-cfc9-4cdc-b32c-6446d4bb476c].owner 3 tagviewmodel[c1ec8d7c-cfc9-4cdc-b32c-6446d4bb476c].ownertype value tagviewmodel.index 9fad7d46-f6f6-4cd7-80be-41e1b5011670 tagviewmodel[9fad7d46-f6f6-4cd7-80be-41e1b5011670].name width tagviewmodel[9fad7d46-f6f6-4cd7-80be-41e1b5011670].value 10 - 20 tagviewmodel[9fad7d46-f6f6-4cd7-80be-41e1b5011670].id 2 tagviewmodel[9fad7d46-f6f6-4cd7-80be-41e1b5011670].owner 3 tagviewmodel[9fad7d46-f6f6-4cd7-80be-41e1b5011670].ownertype value tagviewmodel.index 30048799-116c-4eba-9010-580c681e6a5b tagviewmodel[30048799-116c-4eba-9010-580c681e6a5b].name wqe tagviewmodel[30048799-116c-4eba-9010-580c681e6a5b].value qwe tagviewmodel[30048799-116c-4eba-9010-580c681e6a5b].id 0 tagviewmodel[30048799-116c-4eba-9010-580c681e6a5b].owner 0 tagviewmodel[30048799-116c-4eba-9010-580c681e6a5b].ownertype value
but in controllers's edittags action method taglist null. i'm stuck , have no idea how solve problem.
i see reused lot of code blog post. have 1 small error. when use steven's custom helper provide wrong name it:
@using (html.begincollectionitem("tagviewmodel"))
but should be:
@using (html.begincollectionitem("taglist"))
because edittags
action has parameter named taglist
.
you can post collection part of main model. model might that:
public class indexviewmodel { public ienumerable<tagviewmodel> tags { get; set; } }
and partial view:
@using (html.begincollectionitem("tags"))
Comments
Post a Comment