使用POST即可获得额外参数

时间:2014-02-20 06:44:19

标签: html asp.net-mvc-3 asp.net-mvc-4 asp.net-mvc-5

我的模型是带有此元数据的Movie类:

    [MetadataType(typeof(MovieMetadata))]
    public partial class Movie
    {

    }

    class MovieMetadata
    {
        [ScaffoldColumn(false)]
        public int id { get; set; }

        [Required(ErrorMessage = "Title is required")]]
        [StringLength(70, ErrorMessage = "Title must have a lenght less than 70")]
        public string title { get; set; }

        [Required(ErrorMessage = "The realese date of the movie is required")]
        public DateTime releaseDate { get; set; }

        public string storyline { get; set; }

        public Binary poster { get; set; }

        [ScaffoldColumn(false)]
        public Binary trailer { get; set; }
    }

我的创建视图中包含以下代码:

     @using (Html.BeginForm(null, null, FormMethod.Post, new { id = "createForm", enctype = "multipart/form-data" }))
        {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

    <div class="gallMemberBox">

        <div class="rightFormContent">

            <div>
                @Html.LabelFor(model => model.title)

                @Html.EditorFor(model => model.title)
                @Html.ValidationMessageFor(model => model.title)
            </div>

            <div>
                @Html.LabelFor(model => model.releaseDate)

                @Html.EditorFor(model => model.releaseDate)
                @Html.ValidationMessageFor(model => model.releaseDate)
            </div>

            <div>
                @Html.Label("Poster")

                <input name="poster" value="C:" id="poster" type="file" />
            </div>

            <div>
                @Html.Label("Trailer")

                @*  @Html.TextBox("trailer", "", new { type = "file", id="trailer" })*@
                <input name="trailer" value="" id="trailer" type="file" />
            </div>

            <div>
                @Html.LabelFor(model => model.storyline)

                @Html.TextAreaFor(model => model.storyline, new{id="storyline"})
            </div>

            <div>
                @Html.Label("Directors")
                <select class="chosen-select" multiple="" data-placeholder="Select directors">
                    @for(int i = 0; i < directors.Count; i ++)
                    {
                        <option value="@i">@directors[i].name</option>
                    }
                </select>
            </div>

            <div>
                @Html.Label("Actors")
                <select class="chosen-select" multiple="" data-placeholder="Select actors of the Screen">
                    @for(int i = 0; i < actors.Count; i ++)
                    {
                        <option value="@i">@actors[i].name</option>
                    }
                </select>
            </div>

            <div>
                @Html.Label("Writers")
                <select class="chosen-select" multiple="" data-placeholder="Select writers of the Film">
                    @for(int i = 0; i < writers.Count; i ++)
                    {
                        <option value="@i">@writers[i].name</option>
                    }
                </select>
            </div>

            <input type="submit" value="Create" />

        </div>
}

我刚填充title和releaseDate并点击提交(Create),之后在我的ModelState中我有三个参数,title,releaseDate和storyline(我从来没有在这个上加一些值,而且值是一个空字符串。在其他领域,我从来没有提出过价值,而且他们没有提供那个帖子。为什么?以及如何解决这个问题?

    public ActionResult Create([Bind(Exclude = "poster, trailer")]Movie movie, HttpPostedFileBase poster, HttpPostedFileBase trailer)
    {

enter image description here

2 个答案:

答案 0 :(得分:0)

您的字段故事情节已经可以通过ID和名称“故事情节”获得,因为它是由MVC创建的。

删除“id”部分,或使用“@id = ...”。

答案 1 :(得分:0)

你可以写:

<div>
      Storyline
      @Html.TextAreaFor(model => model.storyline)
</div>