当表单提交控制器的操作方法的模型参数为空 [英] Controller action method's model parameter is empty when form submit

查看:111
本文介绍了当表单提交控制器的操作方法的模型参数为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在电影控制器的操作方法ReleasingTheaters,该ReleasingTheaters有两个重载一个是HTTPGET它接受电影Id和做的东西,并返回ReleasingTheaters查看它接受一个ViewModel对象。对ReleasingTheaters查看,当我通过提交表单点击提交按钮,控制器的另一HTTP POST操作方法ReleasingTheaters接受视图模型并保存模型数据到数据库中。

I have an action Method ReleasingTheaters in Movie controller, The ReleasingTheaters has two overload one is httpGet which accepts the Movie Id and do stuff and return the ReleasingTheaters View which accepts a ViewModel object .On the ReleasingTheaters view when I submit the form by clicking the submit button ,controller's another http Post action Method ReleasingTheaters accepts the viewModel and save model data to database.

下面是我的视图模型:

 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查看:

Here is my ReleasingTheaters View:

 @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对象没有价值,视图模型对象的集合属性全是空的什么是错我的code

When I click the submit button the Model object on action method has no value, the viewModel object's collection properties all is empty whats wrong with my code

如何在我的操作方法得到的模型对象的值(集合),因此,我可以保存或更新的值回我的数据库

How do I get the model object values(collection) on my action method hence that I can save or update that values back to my database

推荐答案

您需要更改的foreach 循环,这样的控制是正确命名,并且可以通过绑定的 DefaultModelBinder

You need to change your foreach to a for loop so that the controls are correctly named and can be bound by the 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 属性与新{ID =DateFrom} 等该助手将呈现一个唯一的ID,但你创建重复的ID是无效的HTML

Note also that you should not be setting the id attribute with new { id = "DateFrom" } etc. The helpers will render a unique id but you are creating duplicate id's which is invalid html

这篇关于当表单提交控制器的操作方法的模型参数为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆