asp.net scaffolded generate item - 选择不带数据库选项的标签

时间:2015-11-25 14:35:48

标签: c# asp.net asp.net-mvc entity-framework asp.net-mvc-scaffolding

我尝试重新解释[这个简单的教程] [1]

在图书模型中,我移除了ScaffoldColumn(false)上方的public int AuthorID,以便在我的视图中选择AuthorID

在重复之后,我可以在Books / Create中看到这个选择,但它没有选项。

在我的数据库中,我可以看到,SampleData模型中添加的所有项目都有BookID and AuthorID

为什么它没有导致html?

编辑:

My Book.cs

using System.ComponentModel.DataAnnotations;

namespace ContosoBooks.Models
{
    public class Book
    {

        public int BookID { get; set; }
        [Required]
        public string Title { get; set; }

        public int Year { get; set; }
        [Range(1, 500)]
        public decimal Price { get; set; }

        public string Genre { get; set; }


        public int AuthorID { get; set; }

        // Navigation property
        public virtual Author Author { get; set; }
    }
}

我的作者。

using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace ContosoBooks.Models
{
    public class Author
    {
        public int AuthorID { get; set; }
        [Required]
        [Display(Name = "Last Name")]
        public string LastName { get; set; }

        [Display(Name = "First Name")]
        public string FirstMidName { get; set; }

        public virtual ICollection<Book> Books { get; set; }
    }
}

My Scaffolded生成的View for Books Create(Create.cshtml)

@model ContosoBooks.Models.Book

@{
    ViewData["Title"] = "Create";
}

<h2>Create</h2>

<form asp-action="Create">
    <div class="form-horizontal">
        <h4>Book</h4>
        <hr />
        <div asp-validation-summary="ValidationSummary.ModelOnly" class="text-danger"></div>
        <div class="form-group">
            <label asp-for="AuthorID" class="col-md-2 control-label"></label>
            <div class="col-md-10">
                <select asp-for="AuthorID" class ="form-control"></select>
            </div>
        </div>
        <div class="form-group">
            <label asp-for="Genre" class="col-md-2 control-label"></label>
            <div class="col-md-10">
                <input asp-for="Genre" class="form-control" />
                <span asp-validation-for="Genre" class="text-danger" />
            </div>
        </div>
        <div class="form-group">
            <label asp-for="Price" class="col-md-2 control-label"></label>
            <div class="col-md-10">
                <input asp-for="Price" class="form-control" />
                <span asp-validation-for="Price" class="text-danger" />
            </div>
        </div>
        <div class="form-group">
            <label asp-for="Title" class="col-md-2 control-label"></label>
            <div class="col-md-10">
                <input asp-for="Title" class="form-control" />
                <span asp-validation-for="Title" class="text-danger" />
            </div>
        </div>
        <div class="form-group">
            <label asp-for="Year" class="col-md-2 control-label"></label>
            <div class="col-md-10">
                <input asp-for="Year" class="form-control" />
                <span asp-validation-for="Year" class="text-danger" />
            </div>
        </div>
        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
</form>

<div>
    <a asp-action="Index">Back to List</a>
</div>

@section Scripts {
    <script src="~/lib/jquery/dist/jquery.min.js"></script>
    <script src="~/lib/jquery-validation/dist/jquery.validate.min.js"></script>
    <script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.min.js"></script>
}

  [1]: http://docs.asp.net/en/latest/tutorials/your-first-aspnet-application.html

1 个答案:

答案 0 :(得分:0)

在您的创建页面上,它不会显示ID,因为它没有值,并且应用程序预计在将对象插入数据库时​​将生成ID。