控制器动作方法';表单提交时,s模型参数为空
本文关键字:模型 参数 表单提交 方法 控制器 | 更新日期: 2023-09-27 18:27:48
我在Movie控制器中有一个操作方法ReleaseTheaters,ReleaseTheatres有两个重载,一个是httpGet,它接受Movie Id并执行任务,并返回接受ViewModel对象的ReleaseTherates View,控制器的另一个http操作后方法Release Theaters接受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");
}
这是我的ReleaseingTheaters视图:
@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>
}
我的问题:
当我单击提交按钮时,操作方法上的Model对象没有值,viewModel对象的集合属性全部为空我的代码出了什么问题
如何在操作方法上获取模型对象值(集合),以便将这些值保存或更新回数据库
您需要将foreach
更改为for
循环,以便正确命名控件并可由DefaultModelBinder
绑定
@for (int i = 0; i < Model.ReleasingTheaters.Count; i++)
{
@Html.TextBoxFor(m => m.ReleasingTheaters[i].DateFrom)
....
}
这将产生输入,如
<input type="text" name="ReleasingTheaters[0].DateFrom" id="ReleasingTheaters_0__DateFrom" value="?" >
<input type="text" name="ReleasingTheaters[1].DateFrom" id="ReleasingTheaters_1__DateFrom" value="?" >
还请注意,您不应该使用new { id = "DateFrom" }
等设置id
属性。助手将呈现一个唯一的id,但您正在创建无效的重复id。html