How do I display data from a View in the database. [Resolved]

Posted by Rajeshmoorthy under ASP.NET MVC on 12/4/2015 | Points: 10 | Views : 1725 | Status : [Member] | Replies : 6
Hi,

First thanks to all of them who are trying to help developers.GREAT HELP!!!!!! :)

I have four tables that are linked to each other to their primary keys. How do display the data on the index page with values from each table. Do I create a view in the DB and use it?

regards
Rajesh

Thanks
Rajesh Moorthy



Responses

Posted by: Rajnilari2015 on: 12/4/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 50

Up
1
Down

Resolved
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

Posted by: Rajeshmoorthy on: 12/4/2015 [Member] Starter | Points: 25

Up
0
Down
hi

Thanks for your quick reply. I will try this right away and come back if required

regards
Rajesh

Thanks
Rajesh Moorthy

Rajeshmoorthy, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sheonarayan on: 12/5/2015 [Administrator] HonoraryPlatinum | Points: 25

Up
0
Down
Thanks Rajnilari. Very well articulated example.

@Rajesh, in case you are looking for how to create primary and foreign key relationship in model, read this article http://techfunda.com/Howto/asp-net-mvc/132/primary-key-and-foreign-key-relationship-in-model.

Thanks

Regards,
Sheo Narayan
http://www.dotnetfunda.com

Rajeshmoorthy, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajnilari2015 on: 12/5/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 25

Up
0
Down
Sheo Sir, Thank you very much

--
Thanks & Regards,
RNA Team

Rajeshmoorthy, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Juliehernandez on: 12/8/2015 [Member] Starter | Points: 25

Up
0
Down
Thanks for the informative post...Keep sharing informative posts like this. http://essayssos.com/writing-service/term-paper-writing-services.html

Rajeshmoorthy, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Juliehernandez on: 12/8/2015 [Member] Starter | Points: 25

Up
0
Down
Thanks for the informative post...Keep sharing informative posts like this.
http://essayssos.com/writing-service/term-paper-writing-services.html

Rajeshmoorthy, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response