【发布时间】:2021-02-21 08:02:34
【问题描述】:
我在视图中有两个 DropDownList。当我尝试传递这些参数时,控制器中的方法被调用但参数等于 null。
当我签入浏览器(F12-network)时,我会查看参数 - 它们已发送但在方法中仍然为空
附: 我尝试更改 List 或 Location 以及 JobTitle 或 CommonEntity 上的参数类型,但它不起作用
控制器:
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";
}
查看:
@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>
}
型号:
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; }
}
【问题讨论】:
标签: c# asp.net asp.net-mvc asp.net-core