I'm still learning MVC and am confused. I am converting a windows application for CRUD with a database to an MVC website. I got an entire ViewModel's CRUD working that uses dropdownlist's already and the code is identical but throwing object reference not set errors with it in another page.
Controller
public ActionResult Create()
{
var shiftsEmployees = new ShiftsEmployeesViewModel();
var oEmployees = new CEmployees();
oEmployees.GetActiveEmployees();
shiftsEmployees.Employees = oEmployees;
return View(shiftsEmployees);
}
// POST: Shifts/Create
[HttpPost]
public ActionResult Create(ShiftsEmployeesViewModel shiftsEmployees)
{
try
{
shiftsEmployees.InsertShift();
return RedirectToAction("Index");
}
catch
{
return View();
}
}
View
@model StatTracker.ASPMVC.ViewModels.ShiftsEmployeesViewModel
@{
ViewBag.Title = "Add Shift";
Layout = "~/Views/Shared/_Layout.cshtml";
}
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal text-center">
@Html.ValidationSummary(true, "", new {@class = "text-danger"})
<div class="form-group">
<div class="input-group date col-md-12">
@Html.LabelFor(model => model.Shift.Date, new {@class = "control-label"})
@Html.EditorFor(model => model.Shift.Date, new {@class = "form-control datepicker", placeholder = "Pick a date"})
@Html.ValidationMessageFor(model => model.Shift.Date, "", new {@class = "text-danger"})
@Html.LabelFor(model => model.Employee.FullName, new {@class = "control-label"})
@Html.DropDownListFor(model => model.Employee.Id, new SelectList(Model.Employees.Employees, "Id", "FullName", Model.Employee), null, null)
@Html.ValidationMessageFor(model => model.Employee.Id, "", new {@class = "text-danger"})
@Html.LabelFor(model => model.Shift.Hours, new {@class = "control-label"})
@Html.EditorFor(model => model.Shift.Hours, new {@class = "form-control", placeholder = "Hours worked"})
@Html.ValidationMessageFor(model => model.Shift.Hours, "", new {@class = "text-danger"})
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<input type="submit" value="Add Shift" class="btn btn-default"/>
</div>
</div>
</div>
}
<div class="text-center col-md-12">
@Html.ActionLink("Back to List", "Index")
</div>
ViewModel:
namespace StatTracker.ASPMVC.ViewModels
{
public class ShiftsEmployeesViewModel
{
public CShift Shift { get; set; }
public CEmployee Employee { get; set; }
public CEmployees Employees { get; set; }
public void InsertShift()
{
CShift.InsertShift(hours: Shift.Hours, employeeid: Employee.Id, date: Shift.Date);
}
public void UpdateShift()
{
CShift.UpdateShift(hours: Shift.Hours, employeeid: Employee.Id, date: Shift.Date, shiftid: Shift.Id);
}
}
}
working code with same idea controller
public ActionResult Create()
{
var oSalesEmployeeService = new SalesEmployeeServiceViewModel();
var oServices = new CServices();
oServices.GetServices();
var oEmployees = new CEmployees();
oEmployees.GetActiveEmployees();
oSalesEmployeeService.Employees = oEmployees;
oSalesEmployeeService.Services = oServices;
return View(oSalesEmployeeService);
}
// POST: Sales/Create
[HttpPost]
public ActionResult Create(SalesEmployeeServiceViewModel oSalesEmployeeService)
{
try
{
oSalesEmployeeService.InsertSale();
return RedirectToAction("Index");
}
catch
{
return View();
}
}
viewmodel
using StatTracker.BL;
namespace StatTracker.ASPMVC.ViewModels
{
public class SalesEmployeeServiceViewModel
{
public CSale Sale { get; set; }
public CEmployees Employees { get; set; }
public CServices Services { get; set; }
public CEmployee Employee { get; set; }
public CService Service { get; set; }
public void InsertSale()
{
CSale.InsertSale(service: Service.Id, date: Sale.Date, employee: Employee.Id);
}
public void UpdateSale()
{
CSale.UpdateSale(service: Service.Id, date: Sale.Date, employee: Employee.Id, salesid: Sale.Id);
}
}
}
view
@model StatTracker.ASPMVC.ViewModels.SalesEmployeeServiceViewModel
@{
ViewBag.Title = "Add Sale";
Layout = "~/Views/Shared/_Layout.cshtml";
}
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal text-center">
@Html.ValidationSummary(true, "", new {@class = "text-danger"})
<div class="form-group">
<div class="input-group date col-md-12">
@Html.LabelFor(model => model.Sale.Date, new {@class = "control-label"})
@Html.EditorFor(model => model.Sale.Date, new {@class = "form-control datepicker", placeholder = "Pick a date"})
@Html.ValidationMessageFor(model => model.Sale.Date, "", new {@class = "text-danger"})
@Html.LabelFor(model => model.Employee.FullName, new {@class = "control-label"})
@Html.DropDownListFor(model => model.Employee.Id, new SelectList(Model.Employees.Employees, "Id", "FullName", Model.Employee), null, null)
@Html.ValidationMessageFor(model => model.Employee.Id, "", new {@class = "text-danger"})
@Html.LabelFor(model => model.Service.Description, new {@class = "control-label"})
@Html.DropDownListFor(model => model.Service.Id, new SelectList(Model.Services.Services, "Id", "Description", Model.Service), null, null)
@Html.ValidationMessageFor(model => model.Service.Id, "", new {@class = "text-danger"})
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<input type="submit" value="Add Sale" class="btn btn-default"/>
</div>
</div>
</div>
}
<div class="text-center col-md-12">
@Html.ActionLink("Back to List", "Index")
</div>