1

学习MVC4/C#。只是简单地尝试将电影列表发布回控制器,但它是空的。我是否正确定义了类?

我有以下型号

 namespace videostore.Models
 {

     public class MovieSummary
     {
         public List<Movie> Movies { get; set; }
     }

     public class Movie
     {
         public string name { get; set; }
     }
 }

跟随控制器

    [HttpPost]
    public ActionResult SaveMovies(MovieSummary movieList)
    {

    }

和以下html

 <script type="text/javascript">
     $(document).ready(function () {
        $("form").submit(function (e) {
           e.preventDefault();
           $.post('/SaveMovies', $('form').serialize(), function (data) {
           });
        });
     });
 </script>

 <form>
      <input type="hidden" name="movie" value="Man of Steel" />
      <input type="hidden" name="movie" value="The Dark Knight" />
      <input type="hidden" name="movie" value="Iron Man" />
      <input type="submit" name="submit" value="submit" />
 </form>
4

2 回答 2

0

试试这个..我希望它会工作

<form>
      <input type="hidden" name="movieList[0].name" value="Man of Steel" />
      <input type="hidden" name="movieList[1].name" value="The Dark Knight" />
      <input type="hidden" name="movieList[2].name" value="Iron Man" />
      <input type="submit" name="submit" value="submit" />
 </form>

在此处查看更多详细信息

于 2013-07-04T05:28:05.897 回答
0

控制器不接受这些值可能是因为您的属性名称和控件名称不同。尝试像这样更改您的电影类和表格

 public class Movie
 {
     public string movieName { get; set; }
 }


 <form>
      <input type="hidden" name="movieName" value="Man of Steel" />
      <input type="hidden" name="movieName" value="The Dark Knight" />
      <input type="hidden" name="movieName" value="Iron Man" />
      <input type="submit" name="submit" value="submit" />
 </form>
于 2013-07-04T04:57:13.760 回答