![](/img/trans.png)
[英]Passing two or more parameters from model view to controller in EF Core / ASP.NET Core MVC application
[英]ASP NET Core (MVC) problem with passing parameters from the view to the controller
我在视图中有两个 DropDownList。 当我尝试传递这些参数时,调用控制器中的方法但参数等于空值。
当我检查浏览器(F12-network)时,我会观察参数 - 它们已发送但在方法中仍然为空
PS 我尝试更改 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; }
}
你做错事,
@using (Html.BeginForm("Find", "Hello"))
[HttpPost]
public string Find(FormCollection form)
{
return "Fine";
}
尝试更新如下参数。 有关更多详细信息,请参阅ASP.NET Core 中的模型绑定。
[HttpPost]
public string Find(Location Location, JobTitle JobTitle)
{
return "Fine";
}
或者您可以尝试使用如下所示的CommonEntity
参数。
[HttpPost]
public string Find(CommonEntity commonEntity)
{
var locationTitle = commonEntity.Location.Title;
var jobTitle = commonEntity.JobTitle.Title;
return "Fine";
}
因为您接受的参数名称是answer1
、 answer2
,所以您的视图中应该有一个匹配的名称才能成功绑定。
你可以修改你的前端代码如下( DropDownListFor
到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>
}
您的控制器:
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";
}
}
班级:
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; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.