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 EmpModels = new List{
//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 items = ds.AsEnumerable().Select(row => new EmpModel { Id = row.Field("Id"), Name = row.Field("Name"), Address = row.Field("Address"), }).ToList(); return items;
}
}
}
It's My View Page
@model MvcApplication2.Models.EmpModel
@model List
@{
ViewBag.Title = "Add Employees";
var grid = new WebGrid(source: Model, defaultSort: "Id", rowsPerPage: 3);
}
Add Emp Records
@ViewBag.Message
@using (Html.BeginForm("AddEmp", "Home", FormMethod.Post))
{
Id: | @Html.TextBoxFor(e => e.Id) |
Name: | @Html.TextBoxFor(e => e.Name) |
Address: | @Html.TextBoxFor(e => e.Address) |
Employee List
@grid.GetHtml(
tableStyle: "grid",
headerStyle: "head",
alternatingRowStyle: "alt",
columns: grid.Columns(
grid.Column("Id"),
grid.Column("Name"),
grid.Column("Address", format: @@item.address )
)
)
}
How can i achieve both models in a single page Please remember I'm new in MVC

Pradeep PatelPosted Dec 10, 2013, 11:53 PM
Jignesh TrivediPosted Dec 10, 2013, 7:46 AM
As per my knowledge MVC does not support multiple model.
Do achieve this you have to create concrete model....
namespace MvcApplication2 emps {get;set;}
{
public class Test
{
public MvcApplication2.Models.EmpModel emp {get;set;}
public List
}
}
and in view
@model MvcApplication2.Test
hope this will help you.
Pradeep PatelPosted Dec 10, 2013, 2:32 AM
Thanx A Lot