表单提交时,控制器操作方法的模型参数为空

时间:2014-10-26 08:21:40

标签: c# asp.net-mvc entity-framework

我在Movie控制器中有一个动作方法ReleasingTheaters,ReleasingTheaters有两个重载,一个是httpGet,它接受Movie Id并执行操作并返回接受ViewModel对象的ReleasingTheaters View。当我提交表单时,ReleasingTheaters视图单击提交按钮,控制器的另一个http Post动作方法ReleasingTheaters接受viewModel并将模型数据保存到数据库。

这是我的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");
    }

这是我的释放剧院观点:

 @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对象的集合属性都是空的,我的代码是错误的

如何在我的操作方法上获取模型对象值(集合),以便我可以将这些值保存或更新回我的数据库

1 个答案:

答案 0 :(得分:2)

您需要将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="?" >

另请注意,您不应该使用id等设置new { id = "DateFrom" }属性。帮助程序将呈现唯一ID,但您创建的副本ID是无效的html