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?
asp.net-core-2.0 model-binding
add a comment |
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?
asp.net-core-2.0 model-binding
add a comment |
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?
asp.net-core-2.0 model-binding
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
asp.net-core-2.0 model-binding
asked Nov 19 at 17:31
Alireza Mn
85
85
add a comment |
add a comment |
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();
}
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
add a comment |
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();
}
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
add a comment |
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();
}
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
add a comment |
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();
}
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();
}
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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