Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# MVC3 Dropdownlist在post中为空_C#_Asp.net Mvc 3_Razor_Foreign Keys_Html Select - Fatal编程技术网

C# MVC3 Dropdownlist在post中为空

C# MVC3 Dropdownlist在post中为空,c#,asp.net-mvc-3,razor,foreign-keys,html-select,C#,Asp.net Mvc 3,Razor,Foreign Keys,Html Select,我已经在这方面工作了好几天,但没有任何进展。我的电影模型在我的帖子中不断失败,这是因为我的dropdownlist值对于parentGenre是空的。我检查了firebug,并发布了正确的值。以下是我到目前为止的情况: 电影模式: public class Movies { public Movies() { this.inventory = new HashSet<Inventory>(); this.transactions =

我已经在这方面工作了好几天,但没有任何进展。我的电影模型在我的帖子中不断失败,这是因为我的dropdownlist值对于parentGenre是空的。我检查了firebug,并发布了正确的值。以下是我到目前为止的情况:

电影模式:

  public class Movies
{
    public Movies()
    {
        this.inventory = new HashSet<Inventory>();
        this.transactions = new HashSet<Transactions>();
    }

    [Key]
    public int movieID { get; set; }

    [Required(ErrorMessage = "Title is required")]
    [StringLength(50)]
    public String Title { get; set; }

    [Required(ErrorMessage = "Director is required")]
    [StringLength(30)]
    public String Director { get; set; }

    [Required(ErrorMessage = "An actor is required")]
    [StringLength(30)]
    public String Stars { get; set; }

    [Required(ErrorMessage = "Description is required")]
    [AllowHtml]
    [Column(TypeName = "nvarchar(MAX)")]
    public String Description { get; set; }

    [Required(ErrorMessage = "Genre is required")]
    public virtual Genres parentGenre { get; set; }

    [Required(ErrorMessage = "Duration is required")]
    public int Duration { get; set; }

    [Required(ErrorMessage = "Rating is required")]
    public String Rating { get; set; }

    [Required(ErrorMessage="Release date is required")]
    public DateTime releaseDate { get; set; }

    public ICollection<Inventory> inventory { get; set; }

    public ICollection<Transactions> transactions { get; set; }
}
视图:

总而言之,它点击if语句检查模型是否有效,但由于“parentGenres”为null而失败,即使该值是在firebug中发布的

编辑:整个视图: @模型电影

@using (Html.BeginForm()) {
@Html.ValidationSummary(true, "Movie was not entered in system. Please correct the errors and try again.")
    <div>
        <div class="input-prepend">
            <span class="add-on"><i class="icon-film"></i></span>
            @Html.TextBoxFor(m => m.Title, new { placeholder = "Title" })
            @Html.ValidationMessageFor(m => m.Title)
        </div>

        <div class="input-prepend">
            <span class="add-on"><i class="icon-facetime-video"></i></span>
            @Html.TextBoxFor(m => m.Director, new { placeholder = "Director" })
            @Html.ValidationMessageFor(m => m.Director)
        </div>

        <div class="input-prepend">
            <span class="add-on"><i class="icon-tags"></i></span>
            @Html.DropDownList("parentGenre", String.Empty)
            @Html.ValidationMessageFor(m => m.parentGenre)
        </div>  


        <div class="input-prepend">
            <span class="add-on"><i class="icon-star-empty"></i></span>
            @Html.TextBoxFor(m => m.Stars, new { placeholder = "Actor" })
            @Html.ValidationMessageFor(m => m.Stars)
        </div>



        <div class="input-prepend">
            <span class="add-on"><i class="icon-time"></i></span>
            @Html.TextBoxFor(m => m.Duration, new { @class="maskedDuration", placeholder = "Duration (mins)" })
            @Html.ValidationMessageFor(m => m.Duration)
        </div>

        <div class="input-prepend">
            <span class="add-on"><i class="icon-warning-sign"></i></span>
            @Html.DropDownListFor(m => m.Rating, (SelectList)ViewBag.Ratings)
            @Html.ValidationMessageFor(m => m.Rating)
        </div>



        <div class="input-prepend">
            <span class="add-on"><i class="icon-calendar"></i></span>
            @Html.TextBoxFor(m => m.releaseDate, new { @class="maskedDate", placeholder = "Release Date (mm/dd/yyyy)" })
            @Html.ValidationMessageFor(m => m.releaseDate)
        </div>


        <div class="control-group">
            <div class="input-prepend">
                <span class="add-on"><i class="icon-comment"></i></span>
                @Html.TextAreaFor(m => m.Description, new { placeholder = "Description", @class="input-xxlarge" })
                @Html.ValidationMessageFor(m => m.Description)
            </div>
        </div>

        <p><button class="btn btn-primary" type="submit" value="Submit">Submit</button></p>
        @Html.ValidationSummary()
    </div>
}
@使用(Html.BeginForm()){
@Html.ValidationSummary(true,“未在系统中输入电影。请更正错误并重试。”)
@TextBoxFor(m=>m.Title,新的{placeholder=“Title”})
@Html.ValidationMessageFor(m=>m.Title)
@TextBoxFor(m=>m.Director,新的{placeholder=“Director”})
@Html.ValidationMessageFor(m=>m.Director)
@Html.DropDownList(“parentgreen”,String.Empty)
@Html.ValidationMessageFor(m=>m.parentGene)
@TextBoxFor(m=>m.Stars,新的{placeholder=“Actor”})
@Html.ValidationMessageFor(m=>m.Stars)
@TextBoxFor(m=>m.Duration,新的{@class=“maskedDuration”,placeholder=“Duration(mins)”})
@Html.ValidationMessageFor(m=>m.Duration)
@DropDownListFor(m=>m.Rating,(SelectList)ViewBag.Ratings)
@Html.ValidationMessageFor(m=>m.Rating)
@Html.TextBoxFor(m=>m.releaseDate,新的{@class=“maskedDate”,placeholder=“发布日期(mm/dd/yyyy)”)
@Html.ValidationMessageFor(m=>m.releaseDate)
@text区域(m=>m.Description,新的{placeholder=“Description”,@class=“input xxlarge”})
@Html.ValidationMessageFor(m=>m.Description)
提交

@Html.ValidationSummary() }

尝试将其更改为DropDownList for

@Html.DropDownListFor(m => m.parentGenre, 
                     (SelectList) ViewBag.parentGenre, String.Empty)
除此之外,我强烈建议您也将selectlist的名称更改为parentGenreList

    ViewBag.parentGenreList = new SelectList(movieRepository.Genres, 
                               "genreId", "genreName");
因此,生成的助手调用将是

@Html.DropDownListFor(m => m.parentGenre, 
                     (SelectList) ViewBag.parentGenreList , String.Empty)

原因是,如果“选择列表”和“绑定”属性共享的名称在视图呈现时经常与入站绑定冲突。

addMovie创建选择列表,但不向视图发送模型。你有模型绑定吗?请添加整个视图。是否进行模型绑定?我看不到你的观点?我想因为名字是一样的它会绑定?我不能执行@Html.DropDownListFor(m=>m.parentgrove,String.empty),因为它会抛出一个错误。通常会。但是我不知道你是怎么用lambda的w/o来做绑定的。哦,糟糕,上面没有复制。我有@model MovieRental.Models.Movies。我将在上面编辑我的。数据绑定错误-流派模型不包含属性ParentGene,这对我来说没有任何意义,因为它应该在电影模型中查看。movieRepository.genres返回“get{return db.genres;}我进行了更新,这是相同的错误。这是因为我的存储库正在返回什么吗?当我这样做时,m.intellisense拾取parentGene.OIC,genres应该是一个id(int).你使用的是代码优先EF?是的,但为什么类型会是int?我认为它必须是类型类型,因为它是外键。代码优先是凝灰岩。我玩它是为了好玩,但我所有的项目都是db优先。你是对的,你需要导航,但你只能从下拉列表中获得id,而不是整个对象。
@Html.DropDownListFor(m => m.parentGenre, 
                     (SelectList) ViewBag.parentGenre, String.Empty)
    ViewBag.parentGenreList = new SelectList(movieRepository.Genres, 
                               "genreId", "genreName");
@Html.DropDownListFor(m => m.parentGenre, 
                     (SelectList) ViewBag.parentGenreList , String.Empty)