'Need help creating table inside the table and then link those three ages with the tablelink or take me to next page using save & continue button
I am brand new to asp.net MVC and I am having some issues creating a website.
I want to keep one one controlled which is "Project controller"and it has it own View-> Create,Edit,Details,and Delete but want to add different but I want to add two more views in the same controller which will be HR and Accounting so there will be another table inside Project-> View ->Create and it will have these three tabs name Project, Accounting, HR and this first Project tab inside create can be named as Project where they will fill information according to each table.
How can I make this work? I have attached my controller code, and three of the view pages code below.
ProjectController
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using SNAPAIS.Models;
using SNAPAIS.Utils;
namespace SNAPAIS.Controllers
{
public class ProjectsController : BaseController
{
private AISEntities db = new AISEntities();
// GET: Projects
public ActionResult Index(int id)
{
var projects = db.Projects.Where(p => p.Contract.Id == id).Include(x=> x.Contract).ToList();
ViewBag.ContractId = id;
return View(projects);
}
// GET: Projects/Details/5
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Project project = db.Projects.Find(id);
if (project == null)
{
return HttpNotFound();
}
return View(project);
}
// GET: Projects/Create
public ActionResult Create(int contractId)
{
Project project = new Project();
project.Id = 0;
project.ContractId = contractId;
return View(project);
}
// POST: Projects/Create
// To protect from overposting attacks, enable the specific properties you want to bind to, for
// more details see https://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Id,ContractId,ProjectName,ProjectManager,MODNumber,POP,CurrentYearOfPOP,AwardAmount,FundingBaseYear,OY1,OY2,OY3,OY4,CustomerName,CustomerAddress,CustOfficerName,CustSpeciAddress,CORName,CORAddress,TPOCName,TPOCAddress,IsWDC,IsIDIQ,InvoiceInfo,InvoicePeriod,Schedule,InvoiceSubNote")] Project project)
{
if (ModelState.IsValid)
{
project.CreatedDate = LoginInfo.CurrentDate();
project.CreatedBy = LoginInfo.CurrentUser();
db.Projects.Add(project);
db.SaveChanges();
return RedirectToAction("Accounting", new { projectId = project.Id });
}
return View(project);
}
// GET: Projects/Edit/5
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Project project = db.Projects.Find(id);
if (project == null)
{
return HttpNotFound();
}
ViewBag.ContractId = new SelectList(db.Contracts, "Id", "ContractName", project.ContractId);
return View(project);
}
// POST: Projects/Edit/5
// To protect from overposting attacks, enable the specific properties you want to bind to, for
// more details see https://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "Id,ContractId,ProjectName,ProjectManager,MODNumber,POP,CurrentYearOfPOP,AwardAmount,FundingBaseYear,OY1,OY2,OY3,OY4,CustomerName,CustomerAddress,CustOfficerName,CustSpeciAddress,CORName,CORAddress,TPOCName,TPOCAddress,IsWDC,IsIDIQ,InvoiceInfo,InvoicePeriod,Schedule,InvoiceSubNote,CreatedDate,CreatedBy,ModifiedDate,ModifiedBy")] Project project)
{
if (ModelState.IsValid)
{
db.Entry(project).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.ContractId = new SelectList(db.Contracts, "Id", "ContractName", project.ContractId);
return View(project);
}
// GET: Projects/Delete/5
public ActionResult Delete(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Project project = db.Projects.Find(id);
if (project == null)
{
return HttpNotFound();
}
return View(project);
}
// POST: Projects/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
Project project = db.Projects.Find(id);
db.Projects.Remove(project);
db.SaveChanges();
return RedirectToAction("Index");
}
public ActionResult Accounting(int projectId)
{
Accounting accounting = new Accounting();
accounting.Id = 0;
accounting.ProjectId = projectId;
return View(accounting);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Accounting([Bind(Include = "Id,ProjectId,NewCPProjectNo,ChargeLevelTimesheet,PLCRate,BillingRate")] Accounting accounting)
{
if (ModelState.IsValid)
{
accounting.CreatedBy = LoginInfo.CurrentUser();
accounting.CreatedDate = LoginInfo.CurrentDate();
Project project = db.Projects.Where(x => x.Id == accounting.ProjectId).Single();
db.Accountings.Add(accounting);
db.SaveChanges();
return RedirectToAction("Index", new { id = project.ContractId});
}
return View(accounting);
}
public ActionResult HR(int projectId)
{
HR hR = new HR();
hR.Id = 0;
hR.ProjectId = projectId;
return View(hR);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult HR([Bind(Include = "Id,ProjectId,EmployeeName,Address,StartDate,Organization,CPProjectCode,CSHS,PayRate,AcualRate,ReportingManager,TimesheetApprover,EmailAddress")] HR hR)
{
if (ModelState.IsValid)
{
hR.CreatedBy = LoginInfo.CurrentUser();
hR.CreatedDate = LoginInfo.CurrentDate();
Project project = db.Projects.Where(x => x.Id == hR.ProjectId).Single();
db.HRs.Add(hR);
db.SaveChanges();
return RedirectToAction("Index", new { id = project.ContractId });
}
return View(hR);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}
}
View Code for Project -> Create where I want to create two another table inside it and name them Accounting and HR.
@model SNAPAIS.Models.Project
@{
ViewBag.Title = "Create Project";
Layout = "~/Views/Shared/_Layout.cshtml";
}
@using (Html.BeginForm("Create", "Projects", FormMethod.Post, new { @class = "form-horizontal" }))
{
@Html.AntiForgeryToken()
<div class="row">
<div>
<ul class="nav nav-tabs">
<li role="presentation" class="active"><a href="/Projects/Create">Project</a></li>
<li role="presentation" class="active"><a href="/Accounting">Accounting</a></li>
<li role="presentation" class="active"><a href="/HR">HR</a></li>
</ul>
</div>
<div class="panel panel-default">
<div class="panel-body">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.ContractId)
@Html.HiddenFor(model => model.Id)
<div class="form-group">
@Html.LabelFor(model => model.ProjectName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ProjectName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ProjectName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ProjectManager, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ProjectManager, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ProjectManager, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.MODNumber, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.MODNumber, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.MODNumber, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.POP, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.POP, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.POP, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.CurrentYearOfPOP, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.CurrentYearOfPOP, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.CurrentYearOfPOP, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.AwardAmount, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.AwardAmount, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.AwardAmount, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.FundingBaseYear, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.FundingBaseYear, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.FundingBaseYear, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.OY1, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.OY1, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.OY1, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.OY2, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.OY2, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.OY2, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.OY3, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.OY3, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.OY3, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.OY4, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.OY4, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.OY4, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.CustomerName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.CustomerName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.CustomerName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.CustomerAddress, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.CustomerAddress, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.CustomerAddress, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.CustOfficerName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.CustOfficerName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.CustOfficerName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.CustSpeciAddress, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.CustSpeciAddress, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.CustSpeciAddress, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.CORName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.CORName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.CORName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.TPOCAddress, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.TPOCAddress, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.TPOCAddress, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.IsWDC, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<div class="checkbox">
@Html.EditorFor(model => model.IsWDC)
@Html.ValidationMessageFor(model => model.IsWDC, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.IsIDIQ, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<div class="checkbox">
@Html.EditorFor(model => model.IsIDIQ)
@Html.ValidationMessageFor(model => model.IsIDIQ, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.InvoiceInfo, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.InvoiceInfo, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.InvoiceInfo, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.InvoicePeriod, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.InvoicePeriod, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.InvoicePeriod, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Schedule, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Schedule, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Schedule, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.InvoiceSubNote, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.InvoiceSubNote, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.InvoiceSubNote, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="panel-footer">
<button type="submit" value="save" class="btn btn-default">
Save & Continue
</button>
@Html.ActionLink("Cancel", "Edit", "Projects")
</div>
</div>
</div>
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
Code for Accounting view file
@model SNAPAIS.Models.Accounting
@{
ViewBag.Title = "Add Accounting";
Layout = "~/Views/Shared/_Layout.cshtml";
}
@using (Html.BeginForm("Accounting", "Projects", FormMethod.Post, new { @class = "form-horizontal" }))
{
@Html.AntiForgeryToken()
<div class="row">
<div>
<ul class="nav nav-tabs">
<li role="presentation" class="active"><a href="/Project/Create">Project</a></li>
<li role="presentation" class="active"><a href="/Accounting">Accounting</a></li>
<li role="presentation" class="active"><a href="/HR">HR</a></li>
</ul>
</div>
<div class="panel panel-default">
<div class="panel-body">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.ProjectId)
<div class="form-group">
@Html.LabelFor(model => model.NewCPProjectNo, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.NewCPProjectNo, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.NewCPProjectNo, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="panel-footer">
<button type="submit" value="save" class="btn btn-default">
Save & Continue
</button>
@Html.ActionLink("Back to List", "Index", "Accounting")
</div>
</div>
</div>
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
Code for HR view file
@model SNAPAIS.Models.HR
@{
ViewBag.Title = "Add HR";
Layout = "~/Views/Shared/_Layout.cshtml";
}
@using (Html.BeginForm("HR", "Projects", FormMethod.Post, new { @class = "form-horizontal" }))
{
@Html.AntiForgeryToken()
<div class="row">
<div>
<ul class="nav nav-tabs">
<li role="presentation" class="active"><a href="/Create">Project</a></li>
<li role="presentation" class="active"><a href="/Accounting">Accounting</a></li>
<li role="presentation" class="active"><a href="/HR">HR</a></li>
</ul>
</div>
<div class="panel panel-default">
<div class="panel-body">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.ProjectId)
<div class="form-group">
@Html.LabelFor(model => model.ProjectId, "ProjectId", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("ProjectId", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.ProjectId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.EmailAddress, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.EmailAddress, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.EmailAddress, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="panel-footer">
<button type="submit" value="save" class="btn btn-primary">
Save
</button>
@Html.ActionLink("Back to List", "Index", "HR")
</div>
</div>
</div>
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

