0
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create(string param1, string param2, [Bind("a,b,c")] object x)

I am getting null values for param1 & param2 on form post.

https://localhost:44342/ControllerName/Create?param1=45c18baa03b7414ea15021a5e3ea64a9&param2=37a29a748efc4d0ea0b41000c2f0b33c

.net core version 3.1

using default routing

app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");
                endpoints.MapRazorPages();
            });

Does anyone knows what am i missing?

  • Based on the URL you posted, seems like Form element method is GET not POST. And in your action method, you're explicitly configured HTTP POST. Can you post your HTML code? – Anuraj Dec 15 '19 at 14:31
  • Not reproduce , could you please confirm the post request is sending to the correct action ? – Nan Yu Dec 16 '19 at 06:04
  • action is correct. it works fine if parameters are integers. – Abdul QADIR Dec 19 '19 at 03:47

1 Answers1

0

In order to get values from url and bind their values to object, first of all you should include them in the Bind property and after you should have proper model in order to bind the values properly.

Look code below

        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Create([Bind("param1,param2")] MyClass x)
        {
            //Do anything
        }
        public class MyClass
        {
            public string param1 { get; set; }
            public string param2 { get; set; }
        }

More information you can find in official documentation https://learn.microsoft.com/en-us/aspnet/core/mvc/models/model-binding?view=aspnetcore-3.1

shrzd
  • 102
  • 2
  • 3
  • 6