【发布时间】:2014-12-21 15:53:06
【问题描述】:
我在电影控制器中有一个动作方法 ReleasingTheaters,ReleasingTheaters 有两个重载一个是 httpGet,它接受电影 ID 并做一些事情并返回接受 ViewModel 对象的 ReleasingTheaters 视图。当我提交表单时,在 ReleasingTheaters 视图上点击提交按钮,控制器的另一个 http Post 操作方法 ReleasingTheaters 接受 viewModel 并将模型数据保存到数据库。
这是我的视图模型:
public class MovieReleasingTheatersViewModel
{
public MovieReleasingTheatersViewModel()
{
AvailableStates = new List<SelectListItem>();
AvailableCities = new List<SelectListItem>();
AvailableLocations = new List<SelectListItem>();
ReleasingTheaters = new List<MovieReleasingTheaterModel>();
}
[Display(Name="State")]
public int? StateId { get; set; }
public IList<SelectListItem> AvailableStates { get; set; }
[Display(Name = "City")]
public int? CityId { get; set; }
public IList<SelectListItem> AvailableCities { get; set; }
public int? LocationId { get; set; }
public IList<SelectListItem> AvailableLocations { get; set; }
public IList<MovieReleasingTheaterModel> ReleasingTheaters { get; set; }
}
这是我的控制:
[HttpGet]
public ActionResult ReleasingTheaters(int id)
{
var model = new MovieReleasingTheatersViewModel();
model = PrepareMovieReleasingTheaterModel(0, 0, 0, id);
return View(model);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult ReleasingTheaters(MovieReleasingTheatersViewModel model)
{
// iterate through the model's collection and save or update database
return View("Index");
}
这是我的 ReleasingTheaters 视图:
@model Bookmany.Admin.Models.Movie.MovieReleasingTheatersViewModel
@{
ViewBag.Title = "Releasing Theaters";
}
<h3>Releasing Theaters</h3>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<table class="table">
<tr>
<th>
@Html.DropDownListFor(model => model.StateId, Model.AvailableStates, "Select State")
</th>
<th>
@Html.DropDownListFor(model => model.CityId, Model.AvailableCities, "Select City")
</th>
<th>
@Html.DropDownListFor(model => model.LocationId, Model.AvailableLocations, "Select Location")
</th>
<th></th>
</tr>
</table>
<table class="table">
<thead class="t_head">
<tr>
<th>
Theater
</th>
<th>
Date From
</th>
<th>
Date To
</th>
<th>
Release
</th>
<th></th>
</tr>
</thead>
<tbody class="t_body">
@foreach (var item in Model.ReleasingTheaters)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.TheaterName)
</td>
<td>
@Html.TextBoxFor(modelItem => item.DateFrom, new { id = "DateFrom" })
</td>
<td>
@Html.TextBoxFor(modelItem => item.DateTo, new { id = "DateTo" })
</td>
<td>
@Html.EditorFor(modelItem => item.IsTheaterChecked)
</td>
</tr>
}
</tbody>
</table>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
}
我的问题:
当我单击提交按钮时,动作方法上的模型对象没有值,viewModel 对象的集合属性都是空的,我的代码有什么问题
如何在我的操作方法上获取模型对象值(集合),以便我可以将这些值保存或更新回我的数据库
【问题讨论】:
标签: c# asp.net-mvc entity-framework