-1

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")
}
Vivek P
  • 133
  • 1
  • 12
  • What do you mean "create template does not create HTML helpers for the complex properties" (clearly a scaffolding issue)? Additionally, you can just declare properties without using private fields just like `public string FirstName { get; set; }`. – Tetsuya Yamamoto Jan 11 '17 at 09:11
  • @TetsuyaYamamoto r u saying having private properties causing scaffolding issue ??? – Vivek P Jan 11 '17 at 09:44
  • No, private fields are totally unrelated to this issue. From my view point, your scaffolding issue comes from relationships between model classes (including some class that extending base class, notice which property names are missing in view). – Tetsuya Yamamoto Jan 11 '17 at 09:56
  • @TetsuyaYamamoto if you notice, in the above code, Person class has gender property(which is another complex type). when I tried to genearate view using "Create" scaffolding and I am using **student model** (_**child class**_), the view doesnot have any helper for Gender whereas it has for other simple properties like firstname, lastname from the Person class(**_parent class_**). – Vivek P Jan 11 '17 at 10:15
  • Your question is not clear! – Soheil Alizadeh Jan 11 '17 at 11:41
  • use `public string FirstName { get; set; }` Instead ` public string FirstName {get { return _firstName; } set { _firstName = value;}}` – Soheil Alizadeh Jan 11 '17 at 11:43
  • @SoheilAlizadeh that did not help in any way.... "public string FirstName{get;set;}" – Vivek P Jan 11 '17 at 12:01
  • Please read this @TetsuyaYamamoto if you notice, in the above code, Person class has gender property(which is another complex type). when I tried to genearate view using "Create" scaffolding and I am using student model (child class), the view doesnot have any helper for Gender whereas it has for other simple properties like firstname, lastname from the Person class(parent class). – Vivek P Jan 11 '17 at 12:02
  • and @SoheilAlizadeh I am not facing problem in "firstname" I have problem in "Why scaffolding does not generate Code (while creating the view) for "Gender" where as it does for "firstname".."lastname" etc ? What kind of clarity do you want ? – Vivek P Jan 11 '17 at 12:06
  • I tried reproducing your problem using same structure in VS 2013 with Create option and successfully created `FirstName`, `LastName`, `MiddleName`, `DateofBirth` and `IsDeleted`, but the `Gender` properties are not created. After **extending** `Gender` class into `Person` class, all `Gender` class properties are included into view. – Tetsuya Yamamoto Jan 12 '17 at 01:22

2 Answers2

0

Use primary key and foreign key.İf you want to save database you can entity framework code first or database first.

Define controller post method

0

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 (i.e. bool), IsDeleted used in view refers to Student class instead of Gender.

Tetsuya Yamamoto
  • 24,297
  • 8
  • 39
  • 61