Ritu Singh
Problem:
I am trying to bind a model to a view (more specifically, a partial view). When I debug the code, I can clearly see the correct object being passed to the view, but in browser DevTools, I see the response as an empty form. I do get a view, but the model is not bound to it, so the form is empty.
For your reference, here is the partial view code:
And here is the controller action that handles request and returns the partial view. Mind you, the exact same code works on another project:
I get no errors anywhere. No errors in VS or in browser console. Nothing gets logged to "output" window in VS either. Also, changing debug > exception settings to break on all exceptions did nothing either, so it seems like no exceptions are being thrown anywhere.
Why could model binding be failing? What am I missing?
Solution:
This happened because the project was originally a razor pages app and I added controllers and views later with AddControllersWithViews and MapControllerRoute. Apparently, the configurations needed don't end there to add MVC to a project, and I also needed to manually create a _ViewImports.cshtml file to import some things.
Create _ViewImports.cshtml directly under Views folder, and inside it, import your model namespace with a @using directive, and also add asp.net core mvc tag helpers. It would look like:
asp.net tag helpers is needed to make "asp-for" attributes be recognized and handled by the framework. Now model binding will work.
Suggested blogs:
>How to Create an array based on two other arrays in Php
>How To Create Nested Table Structure In Angular?
>How to Create_function deprecated in PHP?
>How to delete duplicate names from Array in Typescript?
>How to do PHP Decryption from Node.js Encryption
>How to Set up the Android Emulator?
>How to Set up the local environment for Angular development?
>How to solve encoding issue when writing to a text file, with Python?