asp.net mvc - MVC 4 Null Data on Post -


i missing here, , can't life of me figure out what. can populate model fine , send view, on post, of data null or default.

i'm force feeding create /test/create?a1=5&a2=6&shell=7 works fine initialize test entity. post has name , catid other properties null.

any appreciated.

model

namespace myapp.models {     public partial class testentity     {         [displayname("entity id")]         [required]         public int? entityid { get; set; }          [required]         [displayname("entity name")]         public string entityname { get; set; }          [displayname("category id")]         [required]         public int? catid { get; set; }          [displayname("attribute 1")]         public int? attribute1 { get; set; }          [displayname("attribute 2")]         public int? attribute2 { get; set; }             } } 

view

@model myapp.models.testentity  <h2>test</h2> @using (html.beginform()) {      @html.antiforgerytoken()     @html.validationsummary()     <p>         @html.labelfor(model => model.entityname)         @html.textboxfor(model => model.entityname)     </p>     <p>         @html.labelfor(model => model.catid)         @html.textboxfor(model => model.catid)     </p>      <p>         @html.labelfor(model => model.attribute1)         @html.displayfor(model => model.attribute1)     </p>      <p>         @html.labelfor(model => model.attribute2)         @html.displayfor(model => model.attribute2)     </p>     <input type="submit" value="done" /> 

}

controller

using system.web.mvc; using myapp.models;  namespace myapp.controllers {     public class testcontroller : controller     {         //         // get: /test/create         public actionresult create(int? a1, int? a2, int? shell)         {             using (var db = new mydbcontext())             {                 shellentity temp =                     db.shellentities.where(se => se.shellid == shell).firstordefault();              testentity model = new testentity();             model.catid = temp.category; //get category id shell             model.attribute1 = a1;             model.attribute2 = a2;             return view(model);             }         }          //         // post: /test/create         [httppost]         public actionresult create(testentity model)         {             try             {                 //at point model.attribute1 , attribute2 both null                 if (!model.attribute1.hasvalue)                 {                     //wtf???                 }                 return view(model);             }             catch             {                 return view(model);             }         }     } } 

you need use

@html.hiddenfor(model => model.attribute1) 

to pass values controller instead of displayfor()


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 -