控制器不接受模型值

时间:2013-07-04 04:34:39

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

学习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>

2 个答案:

答案 0 :(得分:0)

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

 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>

答案 1 :(得分: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>

here

上查看更多详情