0% found this document useful (0 votes)
31 views13 pages

Mvcapplication Document

The document describes the implementation of an MVC application for managing company data using Entity Framework Core. 1) An MVCcontroller class is defined to represent the database context. It contains DbSet properties for the company, employee, and address entities. 2) The controller connects to this context and uses it to perform CRUD operations on the company data, including methods for listing, adding, updating, and deleting companies. 3) Views are created using models to display and manage the company forms and data tables. JavaScript is used to validate form inputs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views13 pages

Mvcapplication Document

The document describes the implementation of an MVC application for managing company data using Entity Framework Core. 1) An MVCcontroller class is defined to represent the database context. It contains DbSet properties for the company, employee, and address entities. 2) The controller connects to this context and uses it to perform CRUD operations on the company data, including methods for listing, adding, updating, and deleting companies. 3) Views are created using models to display and manage the company forms and data tables. JavaScript is used to validate form inputs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

1) MVCcontroller :

namespace Address_Book.Data
{
public class MVCcontroller :DbContext
{
public MVCcontroller(DbContextOptions options) : base(options)
{
}

//company - class name domain

// companies - table name sql


public DbSet<company> companies { get; set; }
public DbSet<employee> employees { get; set; }
public DbSet<Address> Addresses { get; set; }

2) Domain=> class
3) Program.cs

builder.Services.AddControllersWithViews();
builder.Services.AddDbContext<MVCcontroller>(options => options.
UseSqlServer(builder.Configuration.GetConnectionString("MVCconnection")));

4) appsetting.json

"*",
"ConnectionStrings": {
"MVCconnection": "server=DESKTOP2PFS5TV\\SQL2022;database=Address_Book;Trusted_connection=true;
TrustServerCertificate=True"
}

5) normal view :
*table show*/
<div class="row">
<div class="col-md-12">
<table class="table">
<thead>
<tr>
<th>ID</th>
<th>Comapny Name</th>
<th>Company Address</th>
<th>City</th>
<th>Company Contact_No</th>
<th>Company Email</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var company in Model)
{
<tr>
<td>@company.cmp_Id</td>
<td>@company.cmp_Name</td>
<td>@company.cmp_Address</td>
<td>@company.city</td>
<td>@company.cmp_cno</td>
<td>@company.cmp_Email</td>
<td><a class="btn btn-secondary" asp-controller="company" asp-
action="view" asp-route-id="@company.cmp_Id">Update</a></td>

<td><a class="btn btn-secondary" asp-controller="company" asp-


action="Delete" asp-route-id="@company.cmp_Id" onClick="return confirm('Are you sure you want to delete this
employee?')">Delete</a></td>

</tr>
}
</tbody>
</table>
</div>
</div>
// button

<br>
<a class="btn btn-primary" id="create" class="create" asp-controller="company" asp-
action="cmp_create">Create</a>
<br>
<br>
}
6) controller ne data sathe connect

public class companyController : Controller


{
private readonly MVCcontroller MVCapplication;

public companyController(MVCcontroller _mvcDemoContext)


{
this.MVCapplication = _mvcDemoContext;
}
//end

7) update/add model
8) update /add form in view :
@model Address_Book.Models.Company_Detail.AddCompanyModel

@{
}
<link rel="stylesheet" type="text/css" href="~/css/style.css">

<head>

<script>
function validateForm() {
let a = document.forms["myForm"]["cmp_Name"].value;
if (a == "") {
alert("Company Name must be filled out");
return false;
}
let b = document.forms["myForm"]["cmp_Address"].value;
if (b == "") {
alert("Company Address must be filled out");
return false;
}
let c = document.forms["myForm"]["city"].value;
if (c == "") {
alert("city Name must be filled out");
return false;
}
let d = document.forms["myForm"]["cmp_cno"].value;
if (d == "") {
alert("Contact no must be filled out");
return false;
}
let e = document.forms["myForm"]["cmp_Email"].value;
if (e == "") {
alert("Comapny Email must be filled out");
return false;
}

}
</script>
</head>

<div class="container">
<div class="main">
<br />

<h4 style="color:black; text-align:center; "> <strong>Add Company detail</strong></h4>


<hr />

<form name="myForm" id="form_id" onsubmit="return validateForm()" method="post">

<strong>Company Name :</strong><br />


<input type="text" asp-for="cmp_Name" id="cmp_Name" /><br />

<strong>Company Address :</strong><br />


<input type="text" asp-for="cmp_Address" id="cmp_address" /><br />

<strong>Company City :</strong><br />


<input type="text" asp-for="city" id="city" /><br />

<strong>Company Contact No :</strong><br />


<input type="text" asp-for="cmp_cno" id="cmp_cno" /><br />

<strong>Company Email :</strong><br />


<input type="email" asp-for="cmp_Email" id="cmp_email" /><br />

<input type="checkbox" required><strong>For Remember Info</strong>


<br />
<br />
<input type="submit" value="Submit" class="btn btn-primary">
<input type="submit" value="Cancel" class="btn btn-primary" onclick="if(confirm('Are you
SURE you wish to cancel?')) history.back();">

<br /><br /><br />

<br />

<br />
<hr />
</form>

9) update/add controller

/* create data */
[HttpGet]
public IActionResult cmp_create()
{
return View();
}

[HttpPost]
public async Task<IActionResult> cmp_create(AddCompanyModel addcompanydetail)
{
var company1 = new company()
{
cmp_Id = Guid.NewGuid(),
cmp_Name = addcompanydetail.cmp_Name,
cmp_Address = addcompanydetail.cmp_Address,
city = addcompanydetail.city,
cmp_cno = addcompanydetail.cmp_cno,
cmp_Email = addcompanydetail.cmp_Email,

};

await MVCapplication.companies.AddAsync(company1);
await MVCapplication.SaveChangesAsync();
return RedirectToAction("view_company");
}

/*update database*/
[HttpGet]
public async Task<IActionResult> View(Guid id)
{
var company = await MVCapplication.companies.FirstOrDefaultAsync(x => x.cmp_Id == id);

if (company != null)
{
var viewModel = new UpdateCompanyViewModel()
{
cmp_Id = company.cmp_Id,
cmp_Name = company.cmp_Name,
cmp_Address = company.cmp_Address,
city = company.city,
cmp_cno = company.cmp_cno,
cmp_Email = company.cmp_Email,
};
return View(viewModel);
}

return RedirectToAction("view_company");
}

[HttpPost]

public async Task<IActionResult> view(UpdateCompanyViewModel model)


{
var company = await MVCapplication.companies.FindAsync(model.cmp_Id);
{
if (company != null)
{
company.cmp_Name = model.cmp_Name;
company.cmp_Address = model.cmp_Address;
company.city = model.city;
company.cmp_cno = model.cmp_cno;
company.cmp_Email = model.cmp_Email;

await MVCapplication.SaveChangesAsync();
return RedirectToAction("view_company");
};
return RedirectToAction("view_company");
}
}

10) delete button call karvu


//delete controllwer

public IActionResult Delete(Guid id)


{
//get the cmp with the Id
var cmp = MVCapplication.companies.FirstOrDefault(x => x.cmp_Id == id);
//remove the cmp from the database
MVCapplication.companies.Remove(cmp);
MVCapplication.SaveChanges();
return RedirectToAction("view_company");
}

11) searching pagging


/* serching and paging */ controller
[HttpGet]

public async Task<IActionResult> view_company(string SearchTxt, int page = 1, int pageSize = 3)


{

int totalItems = MVCapplication.companies.Count();


int totalPages = (int)Math.Ceiling((double)totalItems / pageSize);
page = Math.Max(1, Math.Min(totalPages, page));
List<company> companies = MVCapplication.companies.Skip((page - 1) *
pageSize).Take(pageSize).ToList();

ViewBag.TotalPages = totalPages;
ViewBag.CurrentPage = page;

if (!String.IsNullOrEmpty(SearchTxt))
{
var search_companies = await MVCapplication.companies.ToListAsync();
search_companies = MVCapplication.companies.Where(n =>
n.cmp_Name.Contains(SearchTxt)
|| n.cmp_cno.Contains(SearchTxt)
|| n.cmp_Email.Contains(SearchTxt)
|| n.city.Contains(SearchTxt)
|| n.cmp_Address.Contains(SearchTxt)).ToList();
return View(search_companies);

}
else
{

return View(companies);
}
}
--------------------design part serch--------------

/*serch*/
<div class="row">
<div class="col-md-6" asp:action="view_company" asp-controller="company">
<form class="form-horizontal">
<input type="text" name="SearchTxt" class="form-control" placeholder="Search company
Detail here.." style="max-width: 100%" />

</form>
</div>
<div class="col-md-6" >
<a href="view_company">Back To Details</a>
</div>
<br />
<br />
<br />
</div>

--------------------design part paging--------------

@*pagin*@
<div class="pagin">
<table class="table">
<tr>
@if (ViewBag.CurrentPage > 1)
{
@Html.ActionLink("<< Previous", "view_company", new { page =
ViewBag.CurrentPage - 1 })
}

</tr>
<tr>
Page @ViewBag.CurrentPage of @ViewBag.TotalPages
</tr>
<tr>
@if (ViewBag.CurrentPage < ViewBag.TotalPages)
{
@Html.ActionLink("Next >>", "view_company", new { page =
ViewBag.CurrentPage + 1 })
}
</tr>
<tr></tr>

</table>
</div>

12) ---------mapping controller--------


Get :
/*[HttpGet]
public IActionResult cmp_create()
{
return View();
} */

public IActionResult add_create(company company, employee employee)


{
/*get mapping*/
List<company> companys = new List<company>();

companys = MVCapplication.companies.Distinct().ToList();
companys.Insert(0, new company { cmp_Id = Guid.NewGuid(), cmp_Name = "select" });
ViewBag.ListofCompany = companys;

Guid selectvalue = company.cmp_Id;

List<employee> employees = new List<employee>();

employees = MVCapplication.employees.ToList();
employees.Insert(0, new employee { emp_Id = Guid.NewGuid(), emp_Name = "select" });
ViewBag.Listofemployee = employees;

return View();
}

-----post---- logic & dropdown logic

/*[HttpPost]
public async Task<IActionResult> cmp_create(AddCompanyModel addcompanydetail)
{

var company1 = new company()


{
cmp_Id = Guid.NewGuid(),
cmp_Name = addcompanydetail.cmp_Name,
cmp_Address = addcompanydetail.cmp_Address,
city = addcompanydetail.city,
cmp_cno = addcompanydetail.cmp_cno,
cmp_Email = addcompanydetail.cmp_Email,

};

await MVCapplication.companies.AddAsync(company1);
await MVCapplication.SaveChangesAsync();
return RedirectToAction("view_company");
}
*/
[HttpPost]
public async Task<IActionResult> add_create(AddModel addAddressdetail, company company,employee employee)
{
/*post mapping*/
if (company.cmp_Id == null)
{
ModelState.AddModelError("", "select Country");
}

Guid selectvalue = company.cmp_Id;


ViewBag.selectedvalue = company.cmp_Id;

List<company> list = new List<company>();


list = MVCapplication.companies.ToList();
list.Insert(0, new company { cmp_Id = Guid.NewGuid(), cmp_Name = company.cmp_Name });
ViewBag.ListofCompany = list;

// employee dropdown

if (employee.emp_Id == null)
{
ModelState.AddModelError("", "select employee");
}

Guid selectvalueemp = employee.emp_Id;


ViewBag.selectedvalueemp = employee.emp_Id;

List<employee> emplist = new List<employee>();


emplist = MVCapplication.employees.ToList();
emplist.Insert(0, new employee { emp_Id = Guid.NewGuid(), emp_Name = employee.emp_Name });
ViewBag.ListofEmployee = list;

var address1 = new Address()


{
Address_Id = Guid.NewGuid(),
Add_emp_Name = addAddressdetail.Add_emp_Name,
Add_cmp_Name = addAddressdetail.Add_cmp_Name,
city = addAddressdetail.city,
state = addAddressdetail.state,
country = addAddressdetail.country,

};
await MVCapplication.Addresses.AddAsync(address1);
await MVCapplication.SaveChangesAsync();
return RedirectToAction("view_address");
}

----------------Mapping : design code -------------

<strong>Employee Name :</strong><br />

@*mapping*@
<div class="form-group">

<select asp-for="Add_emp_Name"
class="form-control"
asp-items="@(new SelectList(@ViewBag.ListofEmployee,"emp_Name", "emp_Name"))">
</select>
</div>

<strong>Company Name :</strong><br />

@*mapping*@
<div class="form-group">

<select asp-for="Add_cmp_Name"
class="form-control"
asp-items="@(new SelectList(@ViewBag.ListofCompany,"cmp_Name", "cmp_Name"))">
</select>
</div>

You might also like