-2

I got a two DropDownList's in View. When i try pass those parameters, method in controller called but parameters equals a null.

When i check in browser (F12-network) i watch parameters - they are sended but in method still nulls .

P.S. I try change type of parameters on List or Location and JobTitle or CommonEntity, but its doesn't work

Controller:

 public class HelloController: Controller
{
 
    [HttpGet]
    public IActionResult Index()
    {
          
        var locations = new List<Location>()
        {               
               new Location()
               {
                    Id = 0,
                Title = "Russia"
               
            },                
                new Location()
               {
                    Id = 1,
                Title = "Canada"
               }
        };                   
        
        ViewBag.Location = locations;

        var jobs = new List<JobTitle>()
        {
            new JobsTitle()
            {
                Id = 0,
                Title = "Manager"
            } ,
            new JobsTitle()
            {
                Id = 1,
                Title = "Programmer"
            }
        };

        ViewBag.JobTitle = new SelectList(jobs, "Title", "Title");


        return View();
    }

    [HttpPost]
    public string Find(string answer1, string answer2)
    {
        return "Fine";
    }

View:

@using Stargate.Core.Models.CoreEntities
@model CommonEntity

@using (Html.BeginForm())
{


@Html.DropDownListFor(m => m.Location.Title, new SelectList(ViewBag.Location, "Title", "Title"))
@Html.DropDownListFor(m => m.JobTitle.Title, new SelectList(ViewBag.JobTitle, "Title", "Title"))

<button type="submit">Find</button>

}

Models:

public class CommonEntity
{
    public Location Location { get; set; }       
    public JobTitle JobTitle { get; set; }

}


public class JobTitle
{
    public long Id { get; set; }
    public string Title { get; set; }
}

 public class Location
 {
    public long Id { get; set; }
    public string Title { get; set; }
 }
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Mooonah
  • 31
  • 4

3 Answers3

1

you are doing things wrongly,

  • you should correct your cshtml so that when submitting the form, it will target your Find Action,
@using (Html.BeginForm("Find", "Hello"))
  • In your Find Action you should provide in input args resolvable by the DefaultModelBinder, since you don't have a ViewModel to intercept the response, I would suggest that you recieve a FormCollection and you can access your values from there.
[HttpPost]
    public string Find(FormCollection form)
    {
        return "Fine";
    }
Ismail Diari
  • 498
  • 3
  • 8
1

Try updating parameters as below. Please refer Model Binding in ASP.NET Core for more details.

[HttpPost]
public string Find(Location Location, JobTitle JobTitle)
{
    return "Fine";
}

Or you can try with parameter of CommonEntity like below.

[HttpPost]
public string Find(CommonEntity commonEntity)
{
    var locationTitle = commonEntity.Location.Title;
    var jobTitle = commonEntity.JobTitle.Title;
    
    return "Fine";
}
Karan
  • 12,059
  • 3
  • 24
  • 40
  • if i do first your solution i got a error like : System.InvalidOperationException: "Action 'Controllers.HelloController.Find ' has more than one parameter that was specified or inferred as bound from request body. Only one parameter per action may be bound from body. Inspect the following parameters, and use 'FromQueryAttribute' to specify bound from query, 'FromRouteAttribute' to specify bound from route, and 'FromBodyAttribute' for parameters to be bound from body: Location l JobTitle g" – Mooonah Nov 09 '20 at 12:18
  • Then, i try add this to parameters this: public string Find([FromQuery] Location l, [FromQuery] JobTitle g) { return "Fine"; } then error was gone, but that still null – Mooonah Nov 09 '20 at 12:19
  • 1
    `[FromQuery]` won't work because it will try to find values from `query parameters` (i.e. part after `?` from url `localhost:\\URL?Location.Title=Russia&JobTitle.Title=Programmer)`. But in your case form posts values so it could be `[FromBody]` or don't write any annotation so framework will try its best to get values from URL or Body or Query. – Karan Nov 10 '20 at 04:37
1

Because the parameter names you accept are answer1, answer2, you should have a matching name in your view to make it possible to bind successfully.

You can modify your front-end code as follows(DropDownListForto DropDownList):

@model CommonEntity
@using (Html.BeginForm("Find", "Hello"))
{
@Html.DropDownList("answer1", new SelectList(ViewBag.Location, "Title", "Title"))
@Html.DropDownList("answer2", new SelectList(ViewBag.JobTitle, "Title", "Title"))
<button type="submit">Find</button>
}

Your Controller:

public class HelloController : Controller
{
    [HttpGet]
    public IActionResult Index()
    {

        var locations = new List<Location>()
    {
           new Location()
           {
                Id = 0,
            Title = "Russia"

        },
            new Location()
           {
                Id = 1,
            Title = "Canada"
           }
    };

        ViewBag.Location = locations;

        var jobs = new List<JobTitle>()
    {
        new JobTitle()
        {
            Id = 0,
            Title = "Manager"
        } ,
        new JobTitle()
        {
            Id = 1,
            Title = "Programmer"
        }
    };

        ViewBag.JobTitle = jobs;


        return View();
    }

    [HttpPost]
    public string Find(string answer1,string answer2)
    {
        return "Fine";
    }
}

Class:

 public class CommonEntity
{
    public Location Location { get; set; }
    public JobTitle JobTitle { get; set; }

}
public class JobTitle
{
    public long Id { get; set; }
    public string Title { get; set; }
}
public class Location
{
    public long Id { get; set; }
    public string Title { get; set; }
}

Result: enter image description here enter image description here

Yinqiu
  • 6,609
  • 1
  • 6
  • 14