how to bind data to a user defined class asp.net core 2











up vote
0
down vote

favorite












I defined a model look like this



public class X { 
int a;
MyClass b;
}


and I have an action look like this



public IActionResult Test(X x){}


now when user submit data I want to manipulate that data and then assign it to b.
how can I do it?










share|improve this question


























    up vote
    0
    down vote

    favorite












    I defined a model look like this



    public class X { 
    int a;
    MyClass b;
    }


    and I have an action look like this



    public IActionResult Test(X x){}


    now when user submit data I want to manipulate that data and then assign it to b.
    how can I do it?










    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I defined a model look like this



      public class X { 
      int a;
      MyClass b;
      }


      and I have an action look like this



      public IActionResult Test(X x){}


      now when user submit data I want to manipulate that data and then assign it to b.
      how can I do it?










      share|improve this question













      I defined a model look like this



      public class X { 
      int a;
      MyClass b;
      }


      and I have an action look like this



      public IActionResult Test(X x){}


      now when user submit data I want to manipulate that data and then assign it to b.
      how can I do it?







      asp.net-core-2.0 model-binding






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 19 at 17:31









      Alireza Mn

      85




      85
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          You didn't provide the detailed explanation such as how you want to submit data , suppose you have a book model class that contains a child Author property :



              public class Book
          {
          public int id { get; set; }

          public string name { get; set; }

          public Author author { get; set; }
          }


          public class Author {
          public int authorId { get; set; }

          public string authorName { get; set; }
          }


          In your view , using Tag Helpers in forms , you can bind to Author.authorName :



          @model Book

          <form asp-controller="Home" asp-action="RegisterInput" method="post">
          BookID: <input asp-for="id" /> <br />
          BookName: <input asp-for="name" /><br />

          AuthorID: <input asp-for="author.authorId" /> <br />
          AuthorName: <input asp-for="author.authorName" /><br />
          <button type="submit">Register</button>
          </form>


          In controller , you will find the input values will automatically bind to related properties :



              [HttpPost]
          [ValidateAntiForgeryToken]
          public async Task<IActionResult> RegisterInput(Book book)
          {

          return View();
          }





          share|improve this answer





















          • I submit data by a form. MyClass is a complex class and I can not use default TagHelpers assume MyClass is a DateTime (it's totally different with C# DateTime)
            – Alireza Mn
            Nov 23 at 13:02










          • You should provide more details include your codes to help understand your requirement .
            – Nan Yu
            Nov 26 at 1:24











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53379848%2fhow-to-bind-data-to-a-user-defined-class-asp-net-core-2%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote













          You didn't provide the detailed explanation such as how you want to submit data , suppose you have a book model class that contains a child Author property :



              public class Book
          {
          public int id { get; set; }

          public string name { get; set; }

          public Author author { get; set; }
          }


          public class Author {
          public int authorId { get; set; }

          public string authorName { get; set; }
          }


          In your view , using Tag Helpers in forms , you can bind to Author.authorName :



          @model Book

          <form asp-controller="Home" asp-action="RegisterInput" method="post">
          BookID: <input asp-for="id" /> <br />
          BookName: <input asp-for="name" /><br />

          AuthorID: <input asp-for="author.authorId" /> <br />
          AuthorName: <input asp-for="author.authorName" /><br />
          <button type="submit">Register</button>
          </form>


          In controller , you will find the input values will automatically bind to related properties :



              [HttpPost]
          [ValidateAntiForgeryToken]
          public async Task<IActionResult> RegisterInput(Book book)
          {

          return View();
          }





          share|improve this answer





















          • I submit data by a form. MyClass is a complex class and I can not use default TagHelpers assume MyClass is a DateTime (it's totally different with C# DateTime)
            – Alireza Mn
            Nov 23 at 13:02










          • You should provide more details include your codes to help understand your requirement .
            – Nan Yu
            Nov 26 at 1:24















          up vote
          0
          down vote













          You didn't provide the detailed explanation such as how you want to submit data , suppose you have a book model class that contains a child Author property :



              public class Book
          {
          public int id { get; set; }

          public string name { get; set; }

          public Author author { get; set; }
          }


          public class Author {
          public int authorId { get; set; }

          public string authorName { get; set; }
          }


          In your view , using Tag Helpers in forms , you can bind to Author.authorName :



          @model Book

          <form asp-controller="Home" asp-action="RegisterInput" method="post">
          BookID: <input asp-for="id" /> <br />
          BookName: <input asp-for="name" /><br />

          AuthorID: <input asp-for="author.authorId" /> <br />
          AuthorName: <input asp-for="author.authorName" /><br />
          <button type="submit">Register</button>
          </form>


          In controller , you will find the input values will automatically bind to related properties :



              [HttpPost]
          [ValidateAntiForgeryToken]
          public async Task<IActionResult> RegisterInput(Book book)
          {

          return View();
          }





          share|improve this answer





















          • I submit data by a form. MyClass is a complex class and I can not use default TagHelpers assume MyClass is a DateTime (it's totally different with C# DateTime)
            – Alireza Mn
            Nov 23 at 13:02










          • You should provide more details include your codes to help understand your requirement .
            – Nan Yu
            Nov 26 at 1:24













          up vote
          0
          down vote










          up vote
          0
          down vote









          You didn't provide the detailed explanation such as how you want to submit data , suppose you have a book model class that contains a child Author property :



              public class Book
          {
          public int id { get; set; }

          public string name { get; set; }

          public Author author { get; set; }
          }


          public class Author {
          public int authorId { get; set; }

          public string authorName { get; set; }
          }


          In your view , using Tag Helpers in forms , you can bind to Author.authorName :



          @model Book

          <form asp-controller="Home" asp-action="RegisterInput" method="post">
          BookID: <input asp-for="id" /> <br />
          BookName: <input asp-for="name" /><br />

          AuthorID: <input asp-for="author.authorId" /> <br />
          AuthorName: <input asp-for="author.authorName" /><br />
          <button type="submit">Register</button>
          </form>


          In controller , you will find the input values will automatically bind to related properties :



              [HttpPost]
          [ValidateAntiForgeryToken]
          public async Task<IActionResult> RegisterInput(Book book)
          {

          return View();
          }





          share|improve this answer












          You didn't provide the detailed explanation such as how you want to submit data , suppose you have a book model class that contains a child Author property :



              public class Book
          {
          public int id { get; set; }

          public string name { get; set; }

          public Author author { get; set; }
          }


          public class Author {
          public int authorId { get; set; }

          public string authorName { get; set; }
          }


          In your view , using Tag Helpers in forms , you can bind to Author.authorName :



          @model Book

          <form asp-controller="Home" asp-action="RegisterInput" method="post">
          BookID: <input asp-for="id" /> <br />
          BookName: <input asp-for="name" /><br />

          AuthorID: <input asp-for="author.authorId" /> <br />
          AuthorName: <input asp-for="author.authorName" /><br />
          <button type="submit">Register</button>
          </form>


          In controller , you will find the input values will automatically bind to related properties :



              [HttpPost]
          [ValidateAntiForgeryToken]
          public async Task<IActionResult> RegisterInput(Book book)
          {

          return View();
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 20 at 6:24









          Nan Yu

          5,9452648




          5,9452648












          • I submit data by a form. MyClass is a complex class and I can not use default TagHelpers assume MyClass is a DateTime (it's totally different with C# DateTime)
            – Alireza Mn
            Nov 23 at 13:02










          • You should provide more details include your codes to help understand your requirement .
            – Nan Yu
            Nov 26 at 1:24


















          • I submit data by a form. MyClass is a complex class and I can not use default TagHelpers assume MyClass is a DateTime (it's totally different with C# DateTime)
            – Alireza Mn
            Nov 23 at 13:02










          • You should provide more details include your codes to help understand your requirement .
            – Nan Yu
            Nov 26 at 1:24
















          I submit data by a form. MyClass is a complex class and I can not use default TagHelpers assume MyClass is a DateTime (it's totally different with C# DateTime)
          – Alireza Mn
          Nov 23 at 13:02




          I submit data by a form. MyClass is a complex class and I can not use default TagHelpers assume MyClass is a DateTime (it's totally different with C# DateTime)
          – Alireza Mn
          Nov 23 at 13:02












          You should provide more details include your codes to help understand your requirement .
          – Nan Yu
          Nov 26 at 1:24




          You should provide more details include your codes to help understand your requirement .
          – Nan Yu
          Nov 26 at 1:24


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53379848%2fhow-to-bind-data-to-a-user-defined-class-asp-net-core-2%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Costa Masnaga

          Fotorealismo

          Sidney Franklin