asp.net mvc - Pass model to controller -


im tryin ceate simple blog. in view got this:

@using (html.beginform("addblogpost", "home")) <--probably missing here {     foreach (var item in model.blogpost)      {          @html.labelfor(x=>item.title)          @html.textboxfor(x=>item.title)          @html.labelfor(x=>item.text)          @html.textboxfor(x=>item.text)      }       <input type="submit" value="create post"  /> } 

with submit-button, im hoping pass 2 values controller:

 public actionresult addblogpost(blogpost model)         {              blogpost post = new blogpost()             {                 title = model.title,                 text = model.text,             };              ravensession.store(post);             ravensession.savechanges();             return redirecttoaction("index");         } 

creating new blogpost , saving db. problem method receives null. guess im missing somehting silly?

edit:

im not longer passing list of blogposts...:

@using (html.beginform("addblogpost", "home")) {           @html.labelfor(model.title)          @html.textboxfor(model.title)          @html.labelfor(model.text)          @html.textboxfor(model.text)        <input type="submit" value="create post"  /> } 

this not seem right way...

edit 2: these 2 class:

 public class contentpage     {         public contentpage()         {             template = new routetemplate();             parentreference = "";             url = "/";           }          public string parentreference { get; set; }         public routetemplate template { get; set; }         public string url { get; set; }         public bool showinmenu { get; set; }         public string title { get; set; }         public string id { get; set; }          public blogpost blogpost { get; set; }     } 

blog-class:

public class blogpost     {         public string title { get; set; }         public string text { get; set; }     } 

to view pass contentpage, contains instance of blog-post...i cant seem access blogpost in way describing? sorry not being vlear start.

bind view blogpost class , don't use foreach loop. see following code.

    @using (html.beginform("addblogpost", "home"))      {            @html.labelfor(x=>model.title)          @html.textboxfor(x=>model.title)          @html.labelfor(x=>model.text)          @html.textboxfor(x=>model.text)          <input type="submit" value="create post"  /> } 

Comments

Popular posts from this blog

php - render data via PDO::FETCH_FUNC vs loop -

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

The canvas has been tainted by cross-origin data in chrome only -