ASP NET Core Model Binding
Vložit
- čas přidán 3. 04. 2019
- In this video we will discuss Model Binding in ASP.NET Core with examples.
Healthy diet is very important for both body and mind. We want to inspire you to cook and eat healthy. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking.
/ @aarvikitchen5572
Model binding maps data in an HTTP request to controller action method parameters
The action parameters may be simple types such as integers, strings, etc or complex types like Customer, Employee, Order etc.
Text version of the video
csharp-video-tutorials.blogsp...
Slides
csharp-video-tutorials.blogsp...
ASP.NET Core Text Articles & Slides
csharp-video-tutorials.blogsp...
ASP.NET Core Tutorial
• ASP.NET core tutorial ...
Angular, JavaScript, jQuery, Dot Net & SQL Playlists
czcams.com/users/kudvenka... - Věda a technologie
I love when you say: "red squiggly"
Great! I'm from Brazil (we wrotes Brasil), and yours tutorials was help me learn more times that I'can remember! Thank you!
Thank you Venkat sir, you are awesome!
You are the best tutor I've ever seen.
Excellent sir, Thanks a lot for Detail explanation....
Sir. Thank you very much for in detailed explanation.
I have a doubt here,
how is it even able to remember the created data between states in the current tutorial,When I navigate to Index method it still retains the new created data and the same is retained in every state.as we have learnt that its stateless.
Sir , your videos are always great and I have never seen a tutor like you. You are really great. And I love the music gets played end of the every videos of this series simply nice and soothing.
Hello Saranya - Thank you very much for the feedback. Means a lot. I have included all the .NET core tutorial videos, slides and text articles in sequence on the following page. Hope you will find it handy.
www.pragimtech.com/courses/asp-net-core-mvc-tutorial-for-beginners/
When you have some time, can you please leave your rating and valuable feedback on the REVIEWS tab. It really helps us. Thank you. Good luck and all the very best with everything you are doing.
its confusing.
brilliant explanation.
you are awesome teacher , thank you
Thanks Venkat.
you are the king brother.
Great tutorial !
Top Thank you !!!
Sir, nice explanation
Great tutorial.
Thank you very much.
How does the repository store information? Because we used AddSingleton for the Employee repository service. AddSingleton maintains state. If we hadn't, without the use of a database, session variable, etc. the new employee added would have been immediately lost upon clicking create.
Thank you!
I received NullReferenceException because I had AddTransient method in startup.cs instead of AddSingleton. After I changed it the create button started working well, hope it will help someone
Thanks bro
Please make video on azure DevOps
thank you
Thanks Sir :)
Thank you.
question , when you are creating view is that where you bind model or it can automatically understand when controller takes "Student" as parameter to assign model to it . is not view responsible to model binding?
Thanks
Great Lesson As Always...
One Question Though : Every time you add a new employee (object) to the list; the list gets updated even after you do the redirect. The question is where is the list get stored?..is it cached or what? #kudvenkat
It probably has to be just in memory. When he gets into the database model, you will see how it is inserted into the database where you can see it. If you close the solution and reopen it, new users do not persist.
Yes! I was confused about that as well. Voodoo!
In your " Startup.cs " class you have added a service "services.AddSingleton();"
This AddSingleton is responsible for caching your data in your browser. If you delete the cache, your added data will also be removed.
@@peritusdemi5028 Thank you for making it even clearer.
@@peritusdemi5028 - Thank you for sharing this information.
I'm confused one thing that why in the controller we perform everything in the interface but not in the actual object? we have IEmployeeRepository is an interface and MockEmployeeRepository is an object class that implements that interface. Why we don't instantiate the object in the controller but just the interface? New to C# sorry
I need some suggestions. I have been following along learning a lot. This is the first time I have gotten stumped.
My Create Post action does not seem to be getting to the HomeController Post method. There are no build errors. Output shows a lot of threads exiting with error code 0.
I have tried putting Break points on both the Get and Post HomeController methods. The HttpGet Create method is being hit, the HttpPost Create method is not.
How can I further debug this?
Thank you very much. BTW I am using VS 2019 Core 2.2
Nevermind, it was my html. I changed the button for an input type="submit". Works great! TYVM for these tutorials!
Are there benefits to save it to an separated newEmployee object instead of below code?
_employeeRepository.Add(location);
return RedirectToAction("details", new { id = employee.Id });
hi @KudVenkat sir, it would be very helpful if a concept is explained with respect to previously used concepts and techniques in place of this concept and technique to achieve a particular functionality and how this concept is implemented in various languages.
In my case the ViewModel object has all its attributes with null value, not a single value I entered shows on it
Hi Venkat, What is the lifetime of the _employeeList object after creating and adding new Employee object to it?
Think it's just until the app is closed since its only a mock repository.
Sir, actually I want to learn asp.net but I can't take any decisions till now because of some hesitations.
should I need to start with asp.net or asp.net core ?
Hello Wag - ASP .NET Core is a new technology from Microsoft. However, there are still plenty of opportunities with ASP .NET MVC. Lot of ASP .NET MVC concepts are also relevant in ASP .NET Core. So I suggest learn both ASP .NET MVC and ASP .NET Core to expand your skill set. Hope this helps.
@@Csharp-video-tutorialsBlogspot Hello Sir , whether model binding in asp.net mvc same as asp.net core model binding.
Sir u said everything about mvc by using asp dot net core
Hi Venkat ,Department value (drop down value ) is going null with httppost. Could you please help.
Try to debug your code and monitor the error.
Thank you for tutorial. While practicing the session I came across an issue , Parameter ID never receives value , it is holding null value even though Create method has all required details. below is code snippet:
public HomeController(IEmployeeRepository employeeRepository)
{
_employeeRepository = employeeRepository;
}
[HttpPost]
public RedirectToActionResult Create(Employee employee)
{
Employee newEmployee = _employeeRepository.Add(employee);
return RedirectToAction("Details", new { id = newEmployee.Id });
}
[Route("[Controller]/[action]/{id?}")]
public ViewResult Details(int? empId)
{
HomeDetailsViewBased homeDetailsViewBased = new HomeDetailsViewBased();
homeDetailsViewBased.Employee = _employeeRepository.GetEmployee(empId ?? 1);
homeDetailsViewBased.PageTitle = "Employee Details";
return View(homeDetailsViewBased);
}
Any guidance how to overcome empId receiving null will be helpful using .net core 3.1
In the Route you have {Id?}, but in the method parameter you called it 'empId' instead of 'Id'.
Hello Master, please explain me when exactly the new object as a parameter to Create(Employee employee) has been created? In the Create.cshtml file we map the property to an object of a Class Employee but i can't see the new object.
Thanks a lot
Automatically created by Model Binding !
Run the Create(Employee employee) method in debug mode, and follow your code line by line. You will understand how the object have been created.
@@peritusdemi5028 of course but in the code there isnt any new keyword. The object is automatically created by model Binding and passed to the Create method
How the heck is .net core doing that? Where does the info submited in the form gets stored? In memory? For how long?
can anybody explain why do I need to change add transient to singleton?
Make one tutorial for web api using asp. Net core
null reference exception is occured when press create button
how can i solve it?
Same thing happened here
Change the HomeController constructor
Can someone explain to me why it created a new employee without using the instance or the keyword "new", just added it? thx
This is created in your MockEmployeeRepository class with it constructor.
How to fix the error:NullReferenceException. I already use AddSingleton.But Constructor I use with _employeeRepository = new MockEmployeeRepository();
Check you have the below code in the Startup.cs file in the method "ConfigureServices" --
services.AddSingleton();
I have the same code but can't help it.
NullReferenceException: Object reference not set to an instance of an object.
AspNetCore.Views_Home_Details.ExecuteAsync() in Details.cshtml
+
Name:@Model.Employee.Name
sir help me to clear this error
An unhandled exception occurred while processing the request.
NullReferenceException: Object reference not set to an instance of an object.
NullReferenceException: Object reference not set to an instance of an object.
AspNetCore.Views_Home_Details.ExecuteAsync() in Details.cshtml
+
@Model.Employee.Name
//I receive this error message. Can you show some solution for this.Thanks you
did you get the explanation?
Home controller constructor needs to be changed
Same issue for me
sir, when i add employee and next time when i restart the application it does not show me the added employee. what would be the reason pls help me.And it also does not add employee to my list.
When we are restarting our application MockEmployeeRepository constructor is getting called and list is reset to that 3 items which we are adding inside constructor and once application starts so we can add more items through create option but that newly created items will be there till the application is running once we restart it will again come back to original list
@@navinbadlani9211 okay, I got it and thanks for the reply. So what we can do to overcome it.
@@mohammadhilal8508 We will use SQL database and I hope he will cover that part in the coming slides
Use this in your constructor:
public HomeController(IEmployeeRepository employeeRepository)
{
_employeeRepository = employeeRepository;
}
I'm getting redirected to "create/home" instead of" home/details/id" can anyone tell why!
i have the same prolem
Try correcting the RedirectToAction method.
@Model.Employee.Name is null apparently. The Add function doesn't seem to be working for me
did you get reason and solution? I am facing the same issue. so please share with me.
@@MukundPatel home controller constructor needs changing
@@MukundPatel you get it working?
The constructor of the MockEmployeeRepository class already has a new employee list object. So you don't need a new MockEmployeeRepository in HomeController constructor. So use this in your HomeController constructor instead:
public HomeController(IEmployeeRepository employeeRepository)
{
_employeeRepository = employeeRepository;
}
@@peritusdemi5028 I did the same thing but it could not help
Got a null reference exception
I am also getting null reference exception
@@Pasaaydaan change the Home Controller constructor
NullReferenceException: Object reference not set to an instance of an object.
AspNetCore.Views_Home_Details.ExecuteAsync() in Details.cshtml
Change the HomeController constructor
@@lukedodson3441 Inconsistent accessibility: parameter type 'IEmployeeRepository' is less accessible than method 'HomeController.HomeController(ILogger, IEmployeeRepository)
Fixed it haha
@Marc Daniel P. Zornosa what you mean?
A bit confusing. :)
Who have error.
public HomeController(IEmployeeRepository employeeRepository)
{
_employeeRepository = employeeRepository;
}
You shouldn't be getting at this point if you had been following all 40 videos, no change here in this video - It would also help if you actually said what error you're getting.
Thanks. This has solved the NullReferenceException error
Thanks, from this video, your proposed method is working. solving the NullReferenceException error
I have
I got this error
NullReferenceException: Object reference not set to an instance of an object.
AspNetCore.Views_Home_Details.ExecuteAsync() in Details.cshtml, line 9
Line no. 9 code is : @Model.Employee.Name
you find solution?
got any solution for this ?