我试图在MVC中创建一个小型预订系统。现在我已经创建了一个模型(BookingViewModel)一个控制器(BookingController)以及一个包含下拉列表和一个按钮的视图。
用户将完成多个步骤(视图)以完成预订。 BookingController的索引视图是第一步。
所以下一步是座位(用户可以指向他想要坐的座位)。在这里,我考虑创建另一个控制器,并进行索引视图。如何从第一个索引视图获取值到此步骤?
我知道我应该使用Httppost方法,但是现在我收到了错误:
具有密钥' DepartureRoute'的ViewData项目。属于' System.String'但必须是' IEnumerable< SelectListItem>'
因此,根据您在第一步中输入的值,我希望在下一步中使用这些值,以便搜索数据库。
BookingController
namespace BookingMvc.Controllers
{
public class BookingController : Controller
{
// GET: Booking
public ActionResult Index()
{
var vm = new BookingViewModel();
var citiesList = new List<SelectListItem>
{
new SelectListItem { Value = "1", Text = "Oslo to Copenhagen" },
new SelectListItem { Value = "2", Text = "Copenhagen to Oslo" },
new SelectListItem { Value = "2", Text = "Copenhagen to Stockholm" },
new SelectListItem { Value = "3", Text = "Stockholm to Copenhagen" },
new SelectListItem { Value = "1", Text = "Oslo to Stockholm" },
new SelectListItem { Value = "3", Text = "Stockholm to Oslo" },
};
var numberList = new List<SelectListItem>
{
new SelectListItem { Value = "0", Text = "0" },
new SelectListItem { Value = "1", Text = "1" },
new SelectListItem { Value = "2", Text = "2"}
};
vm.Cities = citiesList;
vm.Number = numberList;
return View(vm);
}
[HttpPost]
public ActionResult Index(BookingViewModel vm)
{
if (ModelState.IsValid)
{
}
return View(vm);
}
}
}
BookingViewModel
namespace BookingMvc.Models
{
public class BookingViewModel
{
public string departureroute { get; set; }
public string returnroute { get; set; }
public DateTime departuredate { get; set; }
public DateTime returndate { get; set; }
public int adults { get; set; }
public int childrens { get; set; }
//Enum lists
public IEnumerable<SelectListItem> Cities { get; set; }
public IEnumerable<SelectListItem> Number { get; set; }
}
}
第1步&#34;索引视图&#34;
@using (Html.BeginForm())
{
<div class="col-sm-4">
@Html.Label("Outgoing route") <br />
@Html.DropDownListFor(m => m.departureroute, Model.Cities, new { @class = "dropdownlist" })
<br />
@Html.Label("Departure date") <br />
<div>
@Html.TextBox("Name", null, new { @class = "dropdownlist" })
</div>
<br />
@Html.Label("Adult") <br />
@Html.DropDownListFor(m => m.adults, Model.Number, new { @class = "dropdownlist" })
</div>
<div class="col-sm-4">
@Html.Label("Return route") <br />
@Html.DropDownListFor(m => m.returnroute, Model.Cities, new { @class = "dropdownlist" })
<br />
@Html.Label("Return date") <br />
<div>
@Html.TextBox("Name", null, new { @class = "dropdownlist" })
</div>
<br />
@Html.Label("Children") <br />
@Html.DropDownListFor(m => m.childrens, Model.Number, new { @class = "dropdownlist" })
</div>
<div class="col-sm-4">
<div class="bookinginfomation">
@Html.Label("Booking information") <br />
<button type="submit" class="bookingbutton">Continue</button>
</div>
</div>
}
希望有人可以告诉我应该注意什么才能获得下一步的价值(观点)