简体   繁体   中英

Entityframework +MVC + Scaffolding

I dont want a view model. I have a model as below

public class Person
    {
        #region private variables
        private string _firstName;
        private string _lastName;
        private string _middleName;
        private Gender _gender;
        #endregion
        #region public properties
        public string FirstName
        {
            get { return _firstName; }
            set { _firstName = value; }
        }
        public string LastName
        {
            get { return _lastName; }
            set { _lastName = value; }
        }
        public string MiddleName
        {
            get { return _middleName; }
            set { _middleName = value; }
        }
        public virtual Gender Gender
        {
            get { return _gender; }
            set { _gender = value; }
        }
        #endregion
    }
 public class Student:Person
    {
        #region private variables
        private int _id;
        private DateTime _dateOfBirth;
        private bool _isDeleted;
        private Guradians _guardian;
        #endregion
        #region public properties
        public int ID
        {
            get { return _id; }
            set { _id = value; }
        }
        public DateTime DateOfBirth
        {
            get { return _dateOfBirth; }
            set { _dateOfBirth = value; }
        }
        public bool IsDeleted
        {
            get { return _isDeleted; }
            set { _isDeleted = value; }
        }
        public virtual Guradians Guardian
        {
            get { return _guardian; }
            set { _guardian = value; }
        }
        #endregion
    }
public class Gender
    {
        #region private variable
        private int _id;
        private string _description;
        private bool _isDeleted;
        #endregion
        #region public properties
        public bool IsDeleted
        {
            get { return _isDeleted; }
            set { _isDeleted = value; }
        }

        public string Description
        {
            get { return _description; }
            set { _description = value; }
        }

        public int ID
        {
            get { return _id; }
            set { _id = value; }
        }
        #endregion
    }
public class Guradians:Person
    {
        #region private variable
        private int _id; 
        private Relationship _relationship;
        #endregion
        #region public properties
        public int ID
        {
            get { return _id; }
            set { _id = value; }
        }
        public Relationship Relationship
        {
            get { return _relationship; }
            set { _relationship = value; }
        }

        #endregion
    }
public class Relationship
    {
        #region private variables
        private int _id;
        private string _description;
        #endregion
        #region public properties
        public int ID
        {
            get { return _id; }
            set { _id = value; }
        }


        public string Description
        {
            get { return _description; }
            set { _description = value; }
        }
        #endregion
    }

I am trying to create a view using Student Model, Scaffold template - Create. Create template does not create html helpers for the complex properties. I am new to MVC. Please help.

the view looks like

@model PracticeWebApp.Models.Student

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Index</h2>

@using (Html.BeginForm()) {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>Student</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.DateOfBirth)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.DateOfBirth)
            @Html.ValidationMessageFor(model => model.DateOfBirth)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.IsDeleted)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.IsDeleted)
            @Html.ValidationMessageFor(model => model.IsDeleted)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.FirstName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.FirstName)
            @Html.ValidationMessageFor(model => model.FirstName)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.LastName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.LastName)
            @Html.ValidationMessageFor(model => model.LastName)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.MiddleName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.MiddleName)
            @Html.ValidationMessageFor(model => model.MiddleName)
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

Use and .İf you want to save database you can entity framework or .如果你想保存数据库,你可以先实体框架

Define controller post method

I had took experiments about inheritance effect on scaffolding to create a view from model without data annotations (primary/foreign keys etc.), and found that your issue may be solved by inherit Gender base class into Person class:

public class Person : Gender
{
    public String FirstName { get; set; }
    public String LastName { get; set; }
    public String MiddleName { get; set; }
    public virtual Gender Gender { get; set; }
}

The inheritance includes all Gender member properties into Person class using virtual property reference of Gender class, hence it will generate HTML helpers for Description property:

<div class="editor-label">
    @Html.LabelFor(model => model.Description)
</div>
<div class="editor-field">
    @Html.EditorFor(model => model.Description)
    @Html.ValidationMessageFor(model => model.Description)
</div>

NB: Since both Gender and Student class have IsDeleted property with exactly same data type (ie bool ), IsDeleted used in view refers to Student class instead of Gender .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM