asp.net mvc - C# Model Binding to Custom Classes Not Working -
i having difficulty getting mvc bind model have created. have done quite few times in past successfully. such, not sure why not working in project.
for example, have following view:
@model storywall.viewmodels.viewpostviewmodel @{ viewbag.title = "view post"; } <article class="story"> <header> <h1>@model.story.title</h1> <spann class="text-muted">@model.story.store.storename</span> <h2>posted @model.story.postername</h2> </header> if(@model.story.storyimage != null) { <div class="storyimagewrapper"> <img src="~/img/@model.story.storyimage" /> </div> <p>@model.story.storybody</p> } </article> <div class="commentssection"> <h2>comments</h2> <h3>add comment</h3> <form method="post" class="form-horizontal" name="commentform" action="/view/addcomment"> @html.antiforgerytoken() <input type="hidden" name="newcomment.storyid" value="@model.story.storyid" /> <div class="form-group"><label class="control-label col-sm-2">name </label><div class="col-sm-10">@html.textboxfor(@m => m.newcomment.commentername, new { @class = "form-control", @required = true, @ng_model = "commentername"}) <span class="text-warning" ng-show="commentform.newcomment.commentername.$dirty && commentform.newcomment.commentername.$invalid"> required </span> <span class="text-warning"> @html.validationmessagefor(@m => m.newcomment.commentername) </span></div> </div> <div class="form-group"><label class="control-label col-sm-2">email </label><div class="col-sm-10">@html.textboxfor(@m => m.newcomment.commenteremail, new { @class = "form-control", @required = true, @ng_model = "commenteremail" }) <span class="text-warning" ng-show="commentform.newcomment.commenteremail.$dirty && commentform.newcomment.commenteremail.$invalid"> required </span> <span class="text-warning"> @html.validationmessagefor(@m => m.newcomment.commenteremail) </span></div> </div> <div class="form-group"><label class="control-label col-sm-2">message </label><div class="col-sm-10">@html.textareafor(@m => m.newcomment.commentbody, new { @class = "form-control", @required = true, @ng_model = "commentbody" }) <span class="text-warning" ng-show="commentform.newcomment.commentbody.$dirty && commentform.newcomment.commentbody.$invalid"> required </span> <span class="text-warning"> @html.validationmessagefor(@m => m.newcomment.commentbody) </span></div> </div> <button type="submit" ng-disabled="commentform.$invalid">submit</button> </form> <h3>current comments</h3> @foreach(var comment in @model.story.comments) { <blockquote>@comment.commentbody</blockquote> <span>poster: @comment.commentername on @comment.dateposted.tostring("mm-dd-yyyy")</span> } </div> even though using html.textboxfor() input boxes, binding still not working expected.
this controller. "comment" in second action method not binding correctly; properties null.
public class viewcontroller : controller { storymodel dbcontext = new storymodel(); public actionresult viewpost(int32 postid) { viewpostviewmodel vm = new viewpostviewmodel(); vm.story = dbcontext.stories.firstordefault(s => s.storyid == postid); return view(vm); } [httppost] [validateantiforgerytoken] public actionresult addcomment(comment comment) { if (modelstate.isvalid) { dbcontext.comments.add(comment); dbcontext.savechanges(); return redirecttoaction("viewpost", new { storyid = comment.storyid}); } else { viewpostviewmodel vm = new viewpostviewmodel(); vm.story = dbcontext.stories.firstordefault(s => s.storyid == comment.storyid); vm.newcomment = comment; return view("viewpost", vm); } } } i know not first time similar question has been asked, not find solution solved problem. additionally, stated, have done in past success.
the "new" element in scenaria me angular.js. first time using framework. interfering binding somehow?
if helps, comment model:
public partial class comment { public int commentid { get; set; } public int? storyid { get; set; } public int userid { get; set; } [required] [stringlength(75)] public string commentername { get; set; } [required] [stringlength(75)] public string commenteremail { get; set; } [required] public datetime dateposted { get; set; } [required] public string commentbody { get; set; } public virtual story story { get; set; } public virtual user user { get; set; } } and viewpostviewmodel
public class viewpostviewmodel { public story story { get; set; } public comment newcomment { get; set; } } }
thanks help.
one answer use @html.editorfor()
@html.editorfor(m => m.newcomment) then on folder view placed create new folder called editortemplates view named object type. in case, comment.cshtml
the view ->
@model storywall.viewmodels.comment @html.textboxfor(m => m.commentername) @html.textboxfor(m => m.commenteremail) @html.textareafor(m => m.commentbody) this approach 1 use work lists (useful in surveys or tests) works single item.
another approach add viewmodel since viewmodel doesn't need 1 one mapping of business objects or database models. :)
edit: forgot add. think using approach method receives post have receive whole viewmodel instead of comment. ->
public actionresult addcomment(viewpostviewmodel vm)
Comments
Post a Comment