No. For that reason use the
ViewModel .
In short ,
A view model represents only the data that needs to be display on the view. It will never be like a domain model; however it can be a subset of that.
For example, let's as we have a Person Domain Model as
public class Person : IEntity
{
public int Id { get; set; } // Person's identity
public string FirstName { get; set; } // First name
public string LastName { get; set; } // Last name
public DateTime DOB {get;set;} //DOB
public DateTime CreationDate {get;set;} //Some creation date
}
now let's say, we need to add a new person record. We can create a viewmodel as
public class CreatePersonViewModel
{
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime DOB {get;set;}
}
As can be figure out that this viewmodel contains three of the properties of the person domain model. We can also make out that there is no Id field , CreationDate field in the CreatePersonViewModel.
Obvious question why ?
The reason is that Id, not necessarily, be set from the view; it might be auto generated by the Employee table. In ideal situation we do this.
About CreationDate, which again will be autogenerated by the system.
So, we can make out that, View Model must not be same as domain model (we explained why) .
As far as the question you asked, say you have Employee and Department Domain Model e.g
public class Employee : IEntity
{
public int Id { get; set; } // Employee's identity
public string FirstName { get; set; } // Employee's first name
public string LastName { get; set; } // Employee's last name
public DateTime DateCreated { get; set; } // Employee Creation Date
}
public class Department : IEntity
{
public int Id { get; set; } // Employee's identity
public string DepartmentName { get; set; } // Department name
public string DepartmentCode { get; set; } // Department code
}
The Employee-Department View model should be some what like
public class CreateEmpDeptViewModel
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string FullName { get { return string.concat(FirstName," " ,LastName) } ; set; } //yes it should be in the ViewModel which is a computed property of FirstName + LastName
public string DepartmentName { get; set; }
public string DepartmentCode { get; set; }
}
You can use
Data Annotations for validation purposes in EmpDeptViewModel like
public class CreateEmpDeptViewModel : ViewModelBase
{
[Display(Name = "First Name")]
[Required(ErrorMessage = "First name required")]
public string FirstName { get; set; }
[Display(Name = "Last Name")]
[Required(ErrorMessage = "Last name required")]
public string LastName { get; set; }
public string FullName { get; set; }
[Display(Name = "Department Name")]
[Required(ErrorMessage = "Department name required")]
public string DepartmentName { get; set; }
[Display(Name = "Department Code")]
[Required(ErrorMessage = "Department code required")]
public string DepartmentCode { get; set; }
}
The
controller should be something as under
public class EmpDeptController : Controller
{
private readonly IEmpDeptService empdeptService;
public EmpDeptController(IEmpDeptService empdeptService) //this is for dependency injection
{
this.empdeptService = empdeptService;
}
public ActionResult Create()
{
CreateEmpDeptViewModel viewModel = new CreateEmpDeptViewModel();
return View(viewModel);
}
}
and the
view will resemble as
@model ViewModels.CreateEmpDeptViewModel
<table>
<tr>
<td><b>First Name:</b></td>
<td>@Html.TextBoxFor(x => x.FirstName, new { maxlength = "20", size = "20" })
@Html.ValidationMessageFor(x => x.FirstName)
</td>
</tr>
<tr>
<td><b>Last Name:</b></td>
<td>@Html.TextBoxFor(x => x.LastName, new { maxlength = "20", size = "20" })
@Html.ValidationMessageFor(x => x.LastName)
</td>
</tr>
.....................................................
.....................................................
<tr>
<td><b>Last Name:</b></td>
<td>@Html.TextBoxFor(x => x.DepartmentCode, new { maxlength = "20", size = "20" })
@Html.ValidationMessageFor(x => x.DepartmentCode)
</td>
</tr>
</table>
Refer to this link
http://techfunda.com/Howto/asp-net-mvc/262/list-data-from-more-than-one-tables-models for a step by step approach. It's a well organized and very informative article written by Mr.Sheo Narayan.
Please ask us in case of any doubt.
Hope this helps
--
Thanks & Regards,
RNA Team
Rajeshmoorthy, if this helps please login to Mark As Answer. | Alert Moderator