C
C#β€’11mo ago
πŸ™ˆ

how to create a record in asp.net mvc based on something passed to the controller form a view.

how to create a record in asp.net mvc based on something passed to the controller form a view.
24 Replies
Angius
Angiusβ€’11mo ago
What record? A database record?
πŸ™ˆ
πŸ™ˆOPβ€’11mo ago
Yup Like I what to pass a id from a view and then in a controller basically fill up a record with the id that got passed in and also adjust the other feilds
Angius
Angiusβ€’11mo ago
Well that's easy, and any random tutorial should've covered it You need to define the model of data sent to the controller, say
public sealed record PersonData(string Name, string Surname, DateOnly Birthday);
public sealed record PersonData(string Name, string Surname, DateOnly Birthday);
Then take it as a parameter in the controller action, with a proper binding method
[HttpPost]
public async Task<IActionResult> AddPerson([FromForm] PersonData data)
{
// ...
}
[HttpPost]
public async Task<IActionResult> AddPerson([FromForm] PersonData data)
{
// ...
}
Use that data to create the database model And save it in the db
var person = new Person {
Name = data.Name,
Surname = data.Surname,
Birthday = data.Birthday
};
_context.People.Add(person);
await _context.SaveChangesAsync();
var person = new Person {
Name = data.Name,
Surname = data.Surname,
Birthday = data.Birthday
};
_context.People.Add(person);
await _context.SaveChangesAsync();
The _context being injected into the controller from DI
public class PeopleController
{
private readonly ApplicationDbContext _context;
public PeopleController(ApplicationDbContext context) => _context = context;
}
public class PeopleController
{
private readonly ApplicationDbContext _context;
public PeopleController(ApplicationDbContext context) => _context = context;
}
πŸ™ˆ
πŸ™ˆOPβ€’11mo ago
Hi thanks Still struggling Is this in the controller I understood thsi I don’t get what this means Which one is parameters from the form What dose sealed mean Can I use @htmlaction
Angius
Angiusβ€’11mo ago
The record describes the parameters of the form sealed means it cannot be inherited from. You don't have to use it if you don't want to
πŸ™ˆ
πŸ™ˆOPβ€’11mo ago
Can you please tell me why this way doesn’t work and why it is not returning to index page or even adding to the database. @ZZZZZZZZZZZZZZZZZZZZZZZZZ
Angius
Angiusβ€’11mo ago
What's that Index property? Does it contain the name of the view?
πŸ™ˆ
πŸ™ˆOPβ€’11mo ago
What The index is default Orderedfood controller index Should this work why dose it Jsut say peg doesn’t work when run
Angius
Angiusβ€’11mo ago
This
No description
πŸ™ˆ
πŸ™ˆOPβ€’11mo ago
But even if it can’t load the index page it should sitll add to database
Angius
Angiusβ€’11mo ago
You can pass a few things to the View() method Usually, just the data you want to send to the view And the view itself is picked by convention Alternatively, you can pass a string with the name of the view And data, if need be So, what is that Index that you're passing to View()?
πŸ™ˆ
πŸ™ˆOPβ€’11mo ago
Like the data is already there in the index
Angius
Angiusβ€’11mo ago
You can't pass a method as a value to the Index method I'd suggest going through some "ASP.NET Core MVC Basics" kind of a tutorial tbh
πŸ™ˆ
πŸ™ˆOPβ€’11mo ago
No that’s my index
Angius
Angiusβ€’11mo ago
That is your Index method, yes
πŸ™ˆ
πŸ™ˆOPβ€’11mo ago
So how do I return to it
Angius
Angiusβ€’11mo ago
And, as I already told you, that is NOT what you can pass to the View() method RedirectToAction() for example
πŸ™ˆ
πŸ™ˆOPβ€’11mo ago
Ok I see
Angius
Angiusβ€’11mo ago
No, not like that either Read what the error says Read the documentation Go through a tutorial
πŸ™ˆ
πŸ™ˆOPβ€’11mo ago
Method group to string Oh Can you suggest any
πŸ™ˆ
πŸ™ˆOPβ€’11mo ago
Ok thank so much Last question Is the other code to add a new record fine And if I fix the return index thing will it work Or is that all worng as well
Angius
Angiusβ€’11mo ago
It seems fine, yes
πŸ™ˆ
πŸ™ˆOPβ€’11mo ago
goot it to work thank you
Want results from more Discord servers?
Add your server