【发布时间】:2021-12-26 14:26:03
【问题描述】:
当我在一个视图中有多个模型时,您如何将表单数据传递给模型(在我的控制器中使用)?
I used this tutorial for multiple models in a view.
索引视图和表单(简化):
@using FlightBooker
@model dynamic
<form asp-controller="Home" asp-action="Search" method="post">
<div class="form-group">
<label for="fromAirport">Flying from:</label>
<select class="form-control">
@foreach (Airport airport in Model.Airports)
{
<option >@airport.AirportCode</option>
}
</select>
<div class="form-group">
<label for="toAirport">Flying to:</label>
<select class="form-control">
@foreach (Airport airport in Model.Airports)
{
<option>@airport.AirportCode</option>
}
</select>
</div>
<label for="fromAirport">Departure Date:</label>
<br />
<input type="date" / id="date" name="date">
<br />
<label for="fromAirport">No. passengers:</label>
<select class="form-control" id="passengers" name="passengers">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
</select>
<button type="submit" class="btn btn-primary mt-3">Search Flights</button>
</div>
</form>
我在看这个教程,这个部分:Strongly type model binding to view。当我使用@model dynamic时,我不知道如何应用它。
或者我会更好地将视图模型创建为suggested here?
【问题讨论】:
-
制作一个名为“SearchViewModel”的ViewModel,添加与输入字段的html名称相同的属性,例如public class SearchViewModel { public datetime date {get;set;} }当你把它作为称为搜索的操作的参数将自动绑定到值。注意:在 html 中添加 select 的值
标签: asp.net-mvc .net-core