Asp.net MVC模型无法查看绑定列表属性

时间:2017-01-06 16:40:44

标签: c# asp.net asp.net-mvc asp.net-mvc-4

我试图填充具有带数据的list属性的模型,但无论我尝试什么,我在控制器中定义的列表数据都不会传递给我的视图。

我有一个看起来像这样的viewmodel:

public class FilmsViewModel
{
    public Films film { get; set; }
    public List<FilmExhibitions> exhibitions { get; set; }

    public FilmsViewModel()
    {
        film = new Films();
        exhibitions = new List<FilmExhibitions>();
    }       
}

然后是一个向其添加一些基础数据的控制器:

public class AdminController : Controller
{
    private IItemsRepository repository = new DbItemsRepository();

    // GET: Admin
    public ActionResult Index()
    {
        return View();
    }

    public ActionResult AddItem()
    {
        return View();
    }


    public ActionResult _AddFilm()
    {
        FilmsViewModel filmsViewModel = new FilmsViewModel();
        filmsViewModel.exhibitions.Add(new FilmExhibitions());
        filmsViewModel.exhibitions.Add(new FilmExhibitions());
        filmsViewModel.exhibitions[0].startTime = new DateTime(2017, 1, 1, 0, 0, 0);
        filmsViewModel.exhibitions[0].endTime = new DateTime(2017, 1, 1, 0, 0, 0);
        filmsViewModel.exhibitions[1].startTime = new DateTime(0001, 1, 1, 0, 0, 0);
        filmsViewModel.exhibitions[1].endTime = new DateTime(0001, 1, 1, 0, 0, 0);

        return PartialView(new FilmsViewModel());
    }

    [HttpPost]
    public ActionResult _AddFilm(FilmsViewModel filmViewModel)
    {
        repository.AddItem(filmViewModel.film);

        Items item = repository.GetItemByName(filmViewModel.film.name);


        return View();
    }

最后我的观点是,我们不想接受这些数据:

@using Project_IHFF.Models
@model FilmsViewModel

@Html.ValidationSummary()
@using (Html.BeginForm("_AddFilm"))
{
    <label>Name: </label>
    @Html.TextBoxFor(x => x.film.name, new { @class = "form-control"})
    <br />
    <label>Description:</label>
    @Html.TextBoxFor(x => x.film.description, new { @class = "form-control" })
    <br />
    <label>Location: </label>
    @Html.TextBoxFor(x => x.film.location, new { @class = "form-control" })
    <br />
    <label>Price: </label>
    @Html.TextBoxFor(x => x.film.price, new { @class = "form-control" })
    <br />
    <label>Directors:</label>
    @Html.TextBoxFor(x => x.film.director, new { @class = "form-control" })
    <br />
    <label>Actors: </label>
    @Html.TextBoxFor(x => x.film.actors, new { @class = "form-control" })
    <br />
    <label>Capacity: </label>
    @Html.TextBoxFor(x => x.film.capacity, new { @class = "form-control" })
    <br />
    <br />
    for (var i = 0; i < Model.exhibitions.Count(); i++)
    {
        @Html.HiddenFor(x => x.exhibitions[i].startTime);
        @Html.HiddenFor(x => x.exhibitions[i].endTime);
        @Html.HiddenFor(x => x.exhibitions[i].id);
    }

    <label>First Exhibition</label><br />
    <label>Starting Time:</label>
    @Html.TextBoxFor(x => x.exhibitions[0].startTime, new { @class = "form-control" })

    <br />
    <label>Ending Time:</label>
    @Html.TextBoxFor(x => x.exhibitions[0].endTime, new { @class = "form-control" })
    <br />
    <br />
    <div id="HiddenDiv">
        <label>Second Exhibition (Optional)</label>
        <br />
        <label>Starting Time:</label>
        @Html.TextBoxFor(x => x.exhibitions[1].startTime, new { @class = "form-control", id = "HiddenTextBox" })
        <br />
        <label>Ending Time:</label>
        @Html.TextBoxFor(x => x.exhibitions[1].endTime, new { @class = "form-control", id = "HiddenTextBox" })
        <br />
    </div>


    <input type="button" id="VisibilityButton" name="show" value="+ Add second exhibition" onclick="showDiv()" class="btn btn-primary" />

    <script>
        function showDiv() {
            document.getElementById('HiddenDiv').style.display = "block";
            document.getElementById('VisibilityButton').value = "- Remove second exhibition";
            document.getElementById('VisibilityButton').onclick = function () { hideDiv() };
        }

        function hideDiv() {
            document.getElementById('HiddenDiv').style.display = "none";
            document.getElementById('VisibilityButton').value = "+ Add second exhibition";
            document.getElementById('VisibilityButton').onclick = function () { showDiv() };
        }
    </script>

    <input type="submit" value="Submit" class="btn btn-primary" />

有谁知道我在这里做错了什么?我此时完全迷失了。

1 个答案:

答案 0 :(得分:2)

_AddFilm控制器中,您将返回PartialView(new FilmsViewModel());,而不是之前创建的FilesViewModel。

我相信你的代码应该是这样的

public ActionResult _AddFilm()
{
    FilmsViewModel filmsViewModel = new FilmsViewModel();
    filmsViewModel.exhibitions.Add(new FilmExhibitions());
    filmsViewModel.exhibitions.Add(new FilmExhibitions());
    filmsViewModel.exhibitions[0].startTime = new DateTime(2017, 1, 1, 0, 0, 0);
    filmsViewModel.exhibitions[0].endTime = new DateTime(2017, 1, 1, 0, 0, 0);
    filmsViewModel.exhibitions[1].startTime = new DateTime(0001, 1, 1, 0, 0, 0);
    filmsViewModel.exhibitions[1].endTime = new DateTime(0001, 1, 1, 0, 0, 0);

    return PartialView(filmsViewModel);
}