I am trying to figure out how to get my data that I've called to my Approve view to post back after any changes have been made. So far the things that I have tried have either thrown an error or flat out erased/disassociated data.
Here is my ViewModel:
public class SurplusRequest
{
public int PickUpID { get; set; }
[DisplayName("First Name:")]
public string EmployeeFN { get; set; }
[DisplayName("Last Name:")]
public string EmployeeLN { get; set; }
[DisplayName("Phone:")]
[DataType(DataType.PhoneNumber)]
[RegularExpression(@"^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$", ErrorMessage = "Not a valid Telephone Number")]
public string EmployeePhone { get; set; }
[DisplayName("Request Date:")]
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}",
ApplyFormatInEditMode = true)]
public DateTime? RequestDate { get; set; }
[DisplayName("Department:")]
public string Department { get; set; }
[DisplayName("Pick Up Requested By:")]
public string PURequestedBy { get; set; }
[DisplayName("Requested Pick Up Date:")]
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}",
ApplyFormatInEditMode = true)]
public DateTime? PURequestedDate { get; set; }
[DisplayName("Division Manager:")]
public string DivisionHead { get; set; }
[DisplayName("Approval Date:")]
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}",
ApplyFormatInEditMode = true)]
public DateTime? DivisionHeadDate { get; set; }
[DisplayName("Surplus Manager:")]
public string SurplusManager { get; set; }
[DisplayName("Surplus Approval Date:")]
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}",
ApplyFormatInEditMode = true)]
public DateTime? SurplusManagerDate { get; set; }
[DisplayName("Financial Officer:")]
public string FinancialOfficer { get; set; }
[DisplayName("Financal Approval Date:")]
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}",
ApplyFormatInEditMode = true)]
public DateTime? FinancialOfficerDate { get; set; }
public PickUpRequest Details { get; set; }
public List<SurplusItem> SurplusItems { get; set; }
public IQueryable<SurplusItem> SItems { get; set; }
}
My Approval View:
<div>
<hr />
<div class="row">
<div class="col-md-4">
@Html.LabelFor(model => model.RequestDate, htmlAttributes: new { @class = "control-label col-md-5" })
@Html.EditorFor(model => model.Details.RequestDate, new { htmlAttributes = new { @class = "form-control", style = "width:90px", @disabled = "disabled" } })
@Html.HiddenFor(model => model.Details.RequestDate)
</div>
</div>
<br />
<div class="row">
<div class="col-md-4">
@Html.LabelFor(model => model.EmployeeFN, htmlAttributes: new { @class = "control-label col-md-5" })
@Html.EditorFor(model => model.Details.EmployeeFN, new { htmlAttributes = new { @class = "form-control", style = "width:175px", @disabled = "disabled" } })
@Html.HiddenFor(model => model.Details.EmployeeFN)
</div>
<div class="col-md-4">
@Html.LabelFor(model => model.EmployeeLN, htmlAttributes: new { @class = "control-label col-md-5" })
@Html.EditorFor(model => model.Details.EmployeeLN, new { htmlAttributes = new { @class = "form-control", style = "width:175px", @disabled = "disabled" } })
@Html.HiddenFor(model => model.Details.EmployeeLN)
</div>
<div class="col-md-4">
@Html.LabelFor(model => model.Department, htmlAttributes: new { @class = "control-label col-md-5" })
@Html.EditorFor(model => model.Details.Department, new { htmlAttributes = new { @class = "form-control", style = "width:175px", @disabled = "disabled" } })
@Html.HiddenFor(model => model.Details.Department)
</div>
</div>
<br />
<div class="row">
<div class="col-md-4">
@Html.LabelFor(model => model.EmployeePhone, htmlAttributes: new { @class = "control-label col-md-5" })
@Html.EditorFor(model => model.Details.EmployeePhone, new { htmlAttributes = new { @class = "form-control", style = "width:150px", @disabled = "disabled" } })
@Html.HiddenFor(model => model.Details.EmployeePhone)
</div>
</div>
</div>
<br />
<br />
<div align="center">
<div class="row">
<table class="table table-bordered table-responsive" style="width:auto">
<tr>
<th>Description of Surplus Item</th>
<th>Asset Tag</th>
<th>Works Y or N</th>
<th>Price Item Is Worth</th>
<th># of Items</th>
<th>Condition of Items</th>
<th>Marked for Trash Y or N</th>
</tr>
@foreach (var item in Model.SItems)
{
<tr>
<td>
@Html.DisplayFor(model => item.Description)
@Html.HiddenFor(model => item.Description)
</td>
<td>
@Html.DisplayFor(model => item.AssetTag)
@Html.HiddenFor(model => item.AssetTag)
</td>
<td>
@Html.DisplayFor(model => item.YesNo1.YesNoChoice)
@Html.HiddenFor(model => item.YesNo1.YesNoChoice)
</td>
<td>
@Html.DisplayFor(model => item.Worth)
@Html.HiddenFor(model => item.Worth)
</td>
<td>
@Html.DisplayFor(model => item.NumItems)
@Html.HiddenFor(model => item.NumItems)
</td>
<td>
@Html.DisplayFor(model => item.Condition.ConditionChoice)
@Html.HiddenFor(model => item.Condition.ConditionChoice)
</td>
<td>
@Html.DisplayFor(model => item.YesNo.YesNoChoice)
@Html.HiddenFor(model => item.YesNo.YesNoChoice)
</td>
</tr>
}
</table>
</div>
</div>
<br />
<br />
<div class="row">
<div class="col-md-5">
@Html.LabelFor(model => model.PURequestedBy, htmlAttributes: new { @class = "col-md-5" })
@Html.EditorFor(model => model.Details.PURequestedBy, new { htmlAttributes = new { @class = "form-control", style = "width:175px", @disabled = "disabled" } })
@Html.HiddenFor(model => model.Details.PURequestedBy)
</div>
<div class="col-md-5">
@Html.LabelFor(model => model.PURequestedDate, htmlAttributes: new { @class = "col-md-6" })
@Html.EditorFor(model => model.Details.PURequestedDate, new { htmlAttributes = new { @class = "form-control", style = "width:90px", @disabled = "disabled" } })
@Html.HiddenFor(model => model.Details.PURequestedDate)
</div>
</div>
<br />
if (Model.Details.DivisionHead == null)
{
<div class="row">
<div class="col-md-5">
@Html.LabelFor(model => model.DivisionHead, htmlAttributes: new { @class = "col-md-5" })
@Html.EditorFor(model => model.Details.DivisionHead, new { htmlAttributes = new { @class = "form-control", style = "width:175px" } })
@Html.ValidationMessageFor(model => model.DivisionHead, "", new { @class = "text-danger" })
</div>
<div class="col-md-5">
@Html.LabelFor(model => model.DivisionHeadDate, htmlAttributes: new { @class = "col-md-6" })
@Html.EditorFor(model => model.Details.DivisionHeadDate, new { htmlAttributes = new { @class = "form-control", style = "width:100px", id = "DivisionHeadDate" } })
@Html.ValidationMessageFor(model => model.DivisionHeadDate, "", new { @class = "text-danger" })
</div>
</div>
}
else if (Model.Details.DivisionHead != null && Model.Details.SurplusManager == null)
{
<div class="row">
<div class="col-md-5">
@Html.LabelFor(model => model.DivisionHead, htmlAttributes: new { @class = "col-md-5" })
@Html.EditorFor(model => model.Details.DivisionHead, new { htmlAttributes = new { @class = "form-control", style = "width:175px", @disabled = "diabled" } })
@Html.HiddenFor(model => model.Details.DivisionHead)
</div>
<div class="col-md-5">
@Html.LabelFor(model => model.DivisionHeadDate, htmlAttributes: new { @class = "col-md-6" })
@Html.EditorFor(model => model.Details.DivisionHeadDate, new { htmlAttributes = new { @class = "form-control", style = "width:90px", id = "DivisionHeadDate", @disabled = "diabled" } })
@Html.HiddenFor(model => model.Details.DivisionHeadDate)
</div>
</div>
<br />
<div class="row">
<div class="col-md-5">
@Html.LabelFor(model => model.SurplusManager, htmlAttributes: new { @class = "col-md-5" })
@Html.EditorFor(model => model.Details.SurplusManager, new { htmlAttributes = new { @class = "form-control", style = "width:175px" } })
@Html.ValidationMessageFor(model => model.SurplusManager, "", new { @class = "text-danger" })
</div>
<div class="col-md-5">
@Html.LabelFor(model => model.SurplusManagerDate, htmlAttributes: new { @class = "col-md-6" })
@Html.EditorFor(model => model.Details.SurplusManagerDate, new { htmlAttributes = new { @class = "form-control", style = "width:90px", id = "SurplusManagerDate" } })
@Html.ValidationMessageFor(model => model.SurplusManagerDate, "", new { @class = "text-danger" })
</div>
</div>
}
else if (Model.Details.DivisionHead != null && Model.Details.SurplusManager != null && Model.Details.FinancialOfficer == null)
{
<div class="row">
<div class="col-md-5">
@Html.LabelFor(model => model.DivisionHead, htmlAttributes: new { @class = "col-md-5" })
@Html.EditorFor(model => model.Details.DivisionHead, new { htmlAttributes = new { @class = "form-control", style = "width:175px", @disabled = "diabled" } })
@Html.HiddenFor(model => model.Details.DivisionHead)
</div>
<div class="col-md-5">
@Html.LabelFor(model => model.DivisionHeadDate, htmlAttributes: new { @class = "col-md-6" })
@Html.EditorFor(model => model.Details.DivisionHeadDate, new { htmlAttributes = new { @class = "form-control", style = "width:90px", id = "DivisionHeadDate", @disabled = "diabled" } })
@Html.HiddenFor(model => model.Details.DivisionHeadDate)
</div>
</div>
<br />
<div class="row">
<div class="col-md-5">
@Html.LabelFor(model => model.SurplusManager, htmlAttributes: new { @class = "col-md-5" })
@Html.EditorFor(model => model.Details.SurplusManager, new { htmlAttributes = new { @class = "form-control", style = "width:175px", @disabled = "disabled" } })
@Html.HiddenFor(model => model.Details.SurplusManager)
</div>
<div class="col-md-5">
@Html.LabelFor(model => model.SurplusManagerDate, htmlAttributes: new { @class = "col-md-6" })
@Html.EditorFor(model => model.Details.SurplusManagerDate, new { htmlAttributes = new { @class = "form-control", style = "width:90px", id = "SurplusManagerDate", @disabled = "disabled" } })
@Html.HiddenFor(model => model.Details.SurplusManagerDate)
</div>
</div>
<br />
<div class="row">
<div class="col-md-5">
@Html.LabelFor(model => model.FinancialOfficer, htmlAttributes: new { @class = "col-md-5" })
@Html.EditorFor(model => model.Details.FinancialOfficer, new { htmlAttributes = new { @class = "form-control", style = "width:175px" } })
@Html.ValidationMessageFor(model => model.FinancialOfficer, "", new { @class = "text-danger" })
</div>
<div class="col-md-5">
@Html.LabelFor(model => model.FinancialOfficerDate, htmlAttributes: new { @class = "col-md-6" })
@Html.EditorFor(model => model.Details.FinancialOfficerDate, new { htmlAttributes = new { @class = "form-control", style = "width:90px", id = "FinancialOfficerDate" } })
@Html.ValidationMessageFor(model => model.FinancialOfficerDate, "", new { @class = "text-danger" })
</div>
</div>
}
else
{
<div class="row">
<div class="col-md-5">
@Html.LabelFor(model => model.DivisionHead, htmlAttributes: new { @class = "col-md-5" })
@Html.EditorFor(model => model.Details.DivisionHead, new { htmlAttributes = new { @class = "form-control", style = "width:175px", @disabled = "diabled" } })
@Html.HiddenFor(model => model.Details.DivisionHead)
</div>
<div class="col-md-5">
@Html.LabelFor(model => model.DivisionHeadDate, htmlAttributes: new { @class = "col-md-6" })
@Html.EditorFor(model => model.Details.DivisionHeadDate, new { htmlAttributes = new { @class = "form-control", style = "width:90px", id = "DivisionHeadDate", @disabled = "diabled" } })
@Html.HiddenFor(model => model.Details.DivisionHeadDate)
</div>
</div>
<br />
<div class="row">
<div class="col-md-5">
@Html.LabelFor(model => model.SurplusManager, htmlAttributes: new { @class = "col-md-5" })
@Html.EditorFor(model => model.Details.SurplusManager, new { htmlAttributes = new { @class = "form-control", style = "width:175px", @disabled = "disabled" } })
@Html.HiddenFor(model => model.Details.SurplusManager)
</div>
<div class="col-md-5">
@Html.LabelFor(model => model.SurplusManagerDate, htmlAttributes: new { @class = "col-md-6" })
@Html.EditorFor(model => model.Details.SurplusManagerDate, new { htmlAttributes = new { @class = "form-control", style = "width:90px", id = "SurplusManagerDate", @disabled = "disabled" } })
@Html.HiddenFor(model => model.Details.SurplusManagerDate)
</div>
</div>
<br />
<div class="row">
<div class="col-md-5">
@Html.LabelFor(model => model.FinancialOfficer, htmlAttributes: new { @class = "col-md-5" })
@Html.EditorFor(model => model.Details.FinancialOfficer, new { htmlAttributes = new { @class = "form-control", style = "width:175px", @disabled = "disabled" } })
@Html.HiddenFor(model => model.Details.FinancialOfficer)
</div>
<div class="col-md-5">
@Html.LabelFor(model => model.FinancialOfficerDate, htmlAttributes: new { @class = "col-md-6" })
@Html.EditorFor(model => model.Details.FinancialOfficerDate, new { htmlAttributes = new { @class = "form-control", style = "width:90px", id = "FinancialOfficerDate", @disabled = "disabled" } })
@Html.HiddenFor(model => model.Details.FinancialOfficerDate)
</div>
</div>
}
<br />
<br />
if (Model.Details.FinancialOfficer == null)
{
<div class="col-md-1">
<input type="submit" value="Approve" class="btn btn-success" />
</div>
<div class="col-md-2">
<input type="button" value="Return" class="btn btn-info" onclick="@("window.location.href='" + @Url.Action("SubmittedRequests", "SurplusRequest") + "'");" />
</div>
}
else
{
<div class="col-md-2">
<input type="button" value="Return" class="btn btn-info" onclick="@("window.location.href='" + @Url.Action("SubmittedRequests", "SurplusRequest") + "'");" />
</div>
}
and my controller method:
public ActionResult Approvals(int? id)
{
var model = new SurplusRequest
{
Details = db.PickUpRequests.Find(id),
SItems = db.SurplusItems.Where(s => s.PickUpID == id)
};
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
if (model == null)
{
return HttpNotFound();
}
return View(model);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Approvals(int? id, SurplusRequest SR)
{
var model = new SurplusRequest
{
Details = db.PickUpRequests.Find(id),
SItems = db.SurplusItems.Where(s => s.PickUpID == id)
};
//SurplusRequest SItems = db.SurplusItems.Where(s => s.PickUpID == id);
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
if (model == null)
{
return HttpNotFound();
}
if (!ModelState.IsValid)
{
return View(model);
}
db.Entry(model.Details).State = EntityState.Modified;
foreach (var i in model.SItems)
{
db.Entry(model.SItems).State = EntityState.Modified;
}
db.SaveChanges();
return RedirectToAction("SubmittedRequests");
}
I'm starting to think that my issue may have something to do with either me trying to use my IQueryable to post the SurplusItem data back is the problem or the fact that I just been to post them back in a different way all together.
Any advice would help. Thanks