Pradeep Patel

Pradeep Patel

  • NA
  • 54
  • 40.6k

I'm unable to add mode than one model in a single page MVC3

Dec 10 2013 2:16 AM
It's a ModelPage

namespace MvcApplication2.Models
{
    public class EmpModel
    {
        int id;
        string name;
        string address;
        string qualification;
        [Required]
        [Display(Name = "Emp Id")]
        public int Id
        {

            get
            {
                return id;
            }

            set
            {
                id = value;
            }

        }
        [Required]
        [Display(Name = "Name")]
        public string Name
        {
            get
            {
                return name;
            }

            set
            {
                name = value;
            }

        }
        [Required]
        [Display(Name = "Address")]
        public string Address
        {
            get
            {
                return address;
            }
            set
            {
                address = value;
            }


        }
        public string Qualification
        {
            get
            {
                return qualification;
            }
            set
            {
                qualification = value;
            }
        }

        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["EmpDbConnectionString"].ConnectionString);
        public IEnumerable GetList()
        {
            //  List<EmpModel> EmpModels = new List<EmpModel>{
            //new EmpModel   { Id=101, Name="Pradeep", Address="Pune"},
            //new EmpModel   { Id=102, Name="Radha", Address="Banglore"},
            //new EmpModel   { Id=103, Name="Priya", Address="Katni"},          
            //  new EmpModel   { Id=104, Name="Abhishek",Address="MH"}
            //  };
            //  return EmpModels;


            SqlCommand cmd = new SqlCommand("Select * From Emp", con);
            DataTable ds = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(ds);

            List<EmpModel> items = ds.AsEnumerable().Select(row => new EmpModel { Id = row.Field<int>("Id"), Name = row.Field<string>("Name"), Address = row.Field<string>("Address"), }).ToList(); return items;
        }
    }
}


It's My View Page


@model  MvcApplication2.Models.EmpModel
@model List<MvcApplication2.Models.EmpModel>
@{
    ViewBag.Title = "Add Employees";
    
    var grid = new WebGrid(source: Model, defaultSort: "Id", rowsPerPage: 3);
}
<h2>
    Add Emp Records</h2>
<p>@ViewBag.Message</p>
@using (Html.BeginForm("AddEmp", "Home", FormMethod.Post))
{
    <table>
        <tr>
            <td>
                <label>
                    Id:</label>
            </td>
            <td>
                @Html.TextBoxFor(e => e.Id)
            </td>
        </tr>
        <tr>
            <td>
                <label>
                    Name:</label>
            </td>
            <td>
                @Html.TextBoxFor(e => e.Name)
            </td>
        </tr>
        <tr>
            <td>
                <label>
                    Address:</label>
            </td>
            <td>
                @Html.TextBoxFor(e => e.Address)
            </td>
        </tr>
        <tr>
            <td colspan="2" align="center">
                <input type="submit" value="Add Record" name="b1" id="btnSubmit" />
                <input type="submit" value="Update Record" name="b1" id="btnUpdate" />
                <input type="submit" value="Delete Record" name="b1" id="btnDelete" />
                <input type="submit" value="Show" name="b1" id="btnShow" />
            </td>
        </tr>
    </table> 
    
    <h2>
    Employee List</h2>
<div id="grid">
    @grid.GetHtml(
        tableStyle: "grid",
        headerStyle: "head",
        alternatingRowStyle: "alt",
        columns: grid.Columns(
            grid.Column("Id"),
            grid.Column("Name"),
            grid.Column("Address", format: @<text>@item.address</text>)
        )
    )
</div>
   
}


How can i achieve both models in a single page Please remember I'm new in MVC

Answers (3)