Hi All I am new to MVC and EntityFramework and am having a problem getting my form to save changes back to the database.
I have the following model
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Services.Providers;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HAZID.Core.Models
{
public class HAZID_Forms : HAZID_Base_Entity
{
[Display(Name = "HAZID_Workers_Id", ResourceType = typeof(Resources.Resource))]
[Required(ErrorMessageResourceName = "HAZID_Workers_Id_Error_Msg", ErrorMessageResourceType = typeof(Resources.Resource))]
public int HAZID_Workers_Id { get; set; }
[Display(Name = "HAZID_Supervisors_Id", ResourceType = typeof(Resources.Resource))]
[Required(ErrorMessageResourceName = "HAZID_Supervisor_Id_Error_Msg", ErrorMessageResourceType = typeof(Resources.Resource))]
public int HAZID_Supervisors_Id { get; set; }
[Display(Name = "HAZID_Form_DateTime", ResourceType = typeof(Resources.Resource))]
public DateTime HAZID_Form_DateTime { get; set; }
[Display(Name = "HAZID_District_Id", ResourceType = typeof(Resources.Resource))]
[Required(ErrorMessageResourceName = "HAZID_District_Id_Error_Msg", ErrorMessageResourceType = typeof(Resources.Resource))]
public int HAZID_District_Id { get; set; }
[Display(Name = "HAZID_Hazard_Location", ResourceType = typeof(Resources.Resource))]
[DataType(DataType.MultilineText)]
[Required(ErrorMessageResourceName = "HAZID_Hazard_Location_Error_Msg", ErrorMessageResourceType = typeof(Resources.Resource))]
public string HAZID_Hazard_Location { get; set; }
[Display(Name = "HAZID_Hazard_Description", ResourceType = typeof(Resources.Resource))]
[DataType(DataType.MultilineText)]
[Required(ErrorMessageResourceName = "HAZID_Hazard_Description_Error_Msg", ErrorMessageResourceType = typeof(Resources.Resource))]
public string HAZID_Hazard_Description { get; set; }
public string HAZID_Hazard_Description_Short { get { return HAZID_Hazard_Description.ToString().SubStringTo(30); } }
[Display(Name = "HAZID_Workers_Suggstion", ResourceType = typeof(Resources.Resource))]
[DataType(DataType.MultilineText)]
public string HAZID_Workers_Suggstion { get; set; }
public string HAZID_Workers_Suggstion_Short { get { return HAZID_Workers_Suggstion.ToString().SubStringTo(30); } }
[Display(Name = "HAZID_Position", ResourceType = typeof(Resources.Resource))]
public string HAZID_Position { get; set; }
[Display(Name = "HAZID_Status_Id", ResourceType = typeof(Resources.Resource))]
public int? HAZID_Status_Id { get; set; }
[Display(Name = "HAZID_Hazard_Type_Id", ResourceType = typeof(Resources.Resource))]
public int? HAZID_Hazard_Type_Id { get; set; }
[Display(Name = "HAZID_Control_Description", ResourceType = typeof(Resources.Resource))]
[DataType(DataType.MultilineText)]
public string HAZID_Control_Description { get; set; }
[Display(Name = "HAZID_Person_Responsible_Name", ResourceType = typeof(Resources.Resource))]
public string HAZID_Person_Responsible_Name { get; set; }
[Display(Name = "HAZID_Applcability_To_Others_Flag", ResourceType = typeof(Resources.Resource))]
public bool HAZID_Applcability_To_Others_Flag { get; set; }
[Display(Name = "HAZID_Risk_Severity_Id", ResourceType = typeof(Resources.Resource))]
public int? HAZID_Risk_Severity_Id { get; set; }
[Display(Name = "HAZID_Risk_Probability_Id", ResourceType = typeof(Resources.Resource))]
public int? HAZID_Risk_Probability_Id { get; set; }
[Display(Name = "HAZID_Action_Taken", ResourceType = typeof(Resources.Resource))]
[DataType(DataType.MultilineText)]
public string HAZID_Action_Taken { get; set; }
}
}
Two ViewModels
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HAZID.Core.Models;
namespace HAZID.Core.ViewModels
{
public class HAZID_Form_View_Model
{
public HAZID_Forms HAZID_Form { get; set; }
public IEnumerable<HAZID_Branch_Districts> HAZID_Branch_Districts { get; set; }
public IEnumerable<HAZID_Persons> HAZID_Persons { get; set; }
public IEnumerable<HAZID_Hazard_Types> HAZID_Hazard_Types { get; set; }
public IEnumerable<HAZID_Risk_Severity_Types> HAZID_Risk_Severity_Types { get; set; }
public IEnumerable<HAZID_Risk_Probability_Types> HAZID_Risk_Probability_Types { get; set; }
public IEnumerable<HAZID_Statuses> HAZID_Statuses { get; set; }
}
}
<pre>using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HAZID.Core.Models;
namespace HAZID.Core.ViewModels
{
public class HAZID_Forms_View_Model
{
public IEnumerable<HAZID_Forms> HAZID_Forms { get; set; }
public IEnumerable<HAZID_Branch_Districts> HAZID_Branch_Districts { get; set; }
public IEnumerable<HAZID_Hazard_Types> HAZID_Hazard_Types { get; set; }
public IEnumerable<HAZID_Risk_Severity_Types> HAZID_Risk_Severity_Types { get; set; }
public IEnumerable<HAZID_Risk_Probability_Types> HAZID_Risk_Probability_Types { get; set; }
public IEnumerable<HAZID_Statuses> HAZID_Statuses { get; set; }
public IEnumerable<HAZID_Persons> HAZID_Persons { get; set; }
}
}
The Controller looks like this
using HAZID.Core.Contracts;
using HAZID.Core.Models;
using HAZID.Core.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace HAZID.WebUI.Controllers
{
public class HAZID_ApplicationController : Controller
{
IHAZID_Repository<HAZID_Forms> hazidForms;
IHAZID_Repository<HAZID_Branch_Districts> hazidBranchDistricts;
IHAZID_Repository<HAZID_Hazard_Types> hazidHazardTypes;
IHAZID_Repository<HAZID_Risk_Severity_Types> hazidRiskSeverityTypes;
IHAZID_Repository<HAZID_Risk_Probability_Types> hazidRiskProbabilityTypes;
IHAZID_Repository<HAZID_Statuses> hazidStatuses;
IHAZID_Repository<HAZID_Persons> hazidPersons;
public HAZID_ApplicationController(IHAZID_Repository<HAZID_Forms> hazidFormContext, IHAZID_Repository<HAZID_Branch_Districts> hazidBranchDistrictContext,
IHAZID_Repository<HAZID_Hazard_Types> hazidHazardTypeContext, IHAZID_Repository<HAZID_Risk_Severity_Types> hazidRiskSeverityTypeContext,
IHAZID_Repository<HAZID_Risk_Probability_Types> hazidRiskProbabilityTypeContext, IHAZID_Repository<HAZID_Statuses> hazidStatusContext,
IHAZID_Repository<HAZID_Persons> hazidPersonContext)
{
this.hazidForms = hazidFormContext;
this.hazidBranchDistricts = hazidBranchDistrictContext;
this.hazidHazardTypes = hazidHazardTypeContext;
this.hazidRiskSeverityTypes = hazidRiskSeverityTypeContext;
this.hazidRiskProbabilityTypes = hazidRiskProbabilityTypeContext;
this.hazidStatuses = hazidStatusContext;
this.hazidPersons = hazidPersonContext;
}
public ActionResult ListReports()
{
HAZID_Forms_View_Model hazidFormsViewModel = new HAZID_Forms_View_Model();
hazidFormsViewModel.HAZID_Forms = hazidForms.Collection().ToList();
hazidFormsViewModel.HAZID_Branch_Districts = hazidBranchDistricts.Collection();
hazidFormsViewModel.HAZID_Hazard_Types = hazidHazardTypes.Collection();
hazidFormsViewModel.HAZID_Risk_Severity_Types = hazidRiskSeverityTypes.Collection();
hazidFormsViewModel.HAZID_Risk_Probability_Types = hazidRiskProbabilityTypes.Collection();
hazidFormsViewModel.HAZID_Statuses = hazidStatuses.Collection();
hazidFormsViewModel.HAZID_Persons = hazidPersons.Collection();
return View(hazidFormsViewModel);
}
public ActionResult EditReport(int id)
{
HAZID_Forms hazidFormToEdit = hazidForms.Find(id);
if (hazidFormToEdit == null)
{
return HttpNotFound();
}
else
{
HAZID_Form_View_Model hazidFormViewModel = new HAZID_Form_View_Model();
hazidFormViewModel.HAZID_Form = hazidFormToEdit;
hazidFormViewModel.HAZID_Branch_Districts = hazidBranchDistricts.Collection();
hazidFormViewModel.HAZID_Hazard_Types = hazidHazardTypes.Collection();
hazidFormViewModel.HAZID_Risk_Severity_Types = hazidRiskSeverityTypes.Collection();
hazidFormViewModel.HAZID_Risk_Probability_Types = hazidRiskProbabilityTypes.Collection();
hazidFormViewModel.HAZID_Statuses = hazidStatuses.Collection();
hazidFormViewModel.HAZID_Persons = hazidPersons.Collection();
return View(hazidFormViewModel);
}
}
[HttpPost]
public ActionResult EditReport(HAZID_Form_View_Model hazidFormViewModel, int id)
{
HAZID_Forms hazidFormToEdit = hazidForms.Find(id);
if (hazidFormToEdit == null)
{
return HttpNotFound();
}
else
{
if (!ModelState.IsValid)
{
return View(hazidFormViewModel);
}
hazidFormToEdit = hazidFormViewModel.HAZID_Form;
hazidForms.Commit();
return RedirectToAction("ListReports");
}
}
public ActionResult ViewForm(int id)
{
HAZID_Forms hazidFormToView = hazidForms.Find(id);
if (hazidFormToView == null)
{
return HttpNotFound();
}
else
{
HAZID_Form_View_Model hazidFormViewModel = new HAZID_Form_View_Model();
hazidFormViewModel.HAZID_Form = hazidFormToView;
hazidFormViewModel.HAZID_Branch_Districts = hazidBranchDistricts.Collection();
hazidFormViewModel.HAZID_Hazard_Types = hazidHazardTypes.Collection();
hazidFormViewModel.HAZID_Risk_Severity_Types = hazidRiskSeverityTypes.Collection();
hazidFormViewModel.HAZID_Risk_Probability_Types = hazidRiskProbabilityTypes.Collection();
hazidFormViewModel.HAZID_Statuses = hazidStatuses.Collection();
hazidFormViewModel.HAZID_Persons = hazidPersons.Collection();
return View(hazidFormViewModel);
}
}
}
}
My Edit view looks like this
@model HAZID.Core.ViewModels.HAZID_Form_View_Model
@{
ViewBag.Title = "Edit Report";
}
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>HASS Report Details</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.HAZID_Form.Id)
@Html.HiddenFor(model => model.HAZID_Form.HAZID_Workers_Id)
@Html.HiddenFor(model => model.HAZID_Form.HAZID_Form_DateTime)
@Html.HiddenFor(model => model.HAZID_Form.HAZID_District_Id)
@Html.HiddenFor(model => model.HAZID_Form.HAZID_Supervisors_Id)
@Html.HiddenFor(model => model.HAZID_Form.HAZID_Hazard_Location)
@Html.HiddenFor(model => model.HAZID_Form.HAZID_Hazard_Description)
@Html.HiddenFor(model => model.HAZID_Form.HAZID_Workers_Suggstion)
<div class="form-group col-md-10">
<div class="row">
<div class="col-3">
@Html.CustomLabelFor(model => model.HAZID_Form.HAZID_Workers_Id)
</div>
<div class="col-7">
@Html.Raw(Model.HAZID_Persons.FirstOrDefault(c => c.Id == Model.HAZID_Form.HAZID_Workers_Id).Full_Name)
</div>
</div>
<div class="row">
<div class="col-3">
@Html.CustomLabelFor(model => model.HAZID_Form.HAZID_Position)
</div>
<div class="col-7">
@Html.Raw(Model.HAZID_Form.HAZID_Position)
</div>
</div>
<div class="row">
<div class="col-3">
@Html.CustomLabelFor(model => model.HAZID_Form.HAZID_Form_DateTime)
</div>
<div class="col-7">
@Html.Raw(Model.HAZID_Form.HAZID_Form_DateTime.ToString())
</div>
</div>
<div class="row">
<div class="col-3">
@Html.CustomLabelFor(model => model.HAZID_Form.HAZID_Supervisors_Id)
</div>
<div class="col-7">
@Html.Raw(Model.HAZID_Persons.FirstOrDefault(c => c.Id == Model.HAZID_Form.HAZID_Supervisors_Id).Full_Name)
</div>
</div>
<div class="row">
@{
var hazidDistrictName = string.Empty;
if (System.Threading.Thread.CurrentThread.CurrentUICulture.Name == "en")
{
hazidDistrictName = Model.HAZID_Branch_Districts.FirstOrDefault(c => c.Id == Model.HAZID_Form.HAZID_District_Id).English_Description.ToString();
}
else
{
hazidDistrictName = Model.HAZID_Branch_Districts.FirstOrDefault(c => c.Id == Model.HAZID_Form.HAZID_District_Id).French_Description.ToString();
}
}
<div class="col-3">
@Html.CustomLabelFor(model => model.HAZID_Form.HAZID_District_Id)
</div>
<div class="col-7">
@Html.Raw(hazidDistrictName)
</div>
</div>
<div class="row">
<div class="col-3">
@Html.CustomLabelFor(model => model.HAZID_Form.HAZID_Hazard_Location)
</div>
<div class="col-7">
@Html.Raw(Model.HAZID_Form.HAZID_Hazard_Location)
</div>
</div>
<div class="row">
<div class="col-3">
@Html.CustomLabelFor(model => model.HAZID_Form.HAZID_Hazard_Description)
</div>
<div class="col-7">
@Html.Raw(Model.HAZID_Form.HAZID_Hazard_Description)
</div>
</div>
<div class="row">
<div class="col-3">
@Html.CustomLabelFor(model => model.HAZID_Form.HAZID_Workers_Suggstion)
</div>
<div class="col-7">
@Html.Raw(Model.HAZID_Form.HAZID_Workers_Suggstion)
</div>
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.HAZID_Form.HAZID_Status_Id, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-3">
@Html.DropDownListFor(model => model.HAZID_Form.HAZID_Status_Id, new SelectList(Model.HAZID_Statuses, "Id", Resources.Resource.HAZID_Column_Select), Resources.Resource.HAZID_Status_Id_Select, new { htmlAttributes = new { @class = "form-control", id = "HAZID_Form_HAZID_Status_Id" } })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.HAZID_Form.HAZID_Action_Taken, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.HAZID_Form.HAZID_Action_Taken, new { htmlAttributes = new { @class = "form-control", @style = "width:100% !important; min-width:600px; height:100px" } })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.HAZID_Form.HAZID_Hazard_Type_Id, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-4">
@Html.DropDownListFor(model => model.HAZID_Form.HAZID_Hazard_Type_Id, new SelectList(Model.HAZID_Hazard_Types, "Id", Resources.Resource.HAZID_Column_Select), Resources.Resource.HAZID_Hazard_Type_Select, new { htmlAttributes = new { @class = "form-control", id = "HAZID_Form_HAZID_Hazard_Type_Id" } })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.HAZID_Form.HAZID_Risk_Severity_Id, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-4">
@Html.DropDownListFor(model => model.HAZID_Form.HAZID_Risk_Severity_Id, new SelectList(Model.HAZID_Risk_Severity_Types, "Id", Resources.Resource.HAZID_Column_Select), Resources.Resource.HAZID_Risk_Severity_Id_Select, new { htmlAttributes = new { @class = "form-control", id = "HAZID_Form_HAZID_Risk_Severity_Id" } })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.HAZID_Form.HAZID_Hazard_Type_Id, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-4">
@Html.DropDownListFor(model => model.HAZID_Form.HAZID_Risk_Probability_Id, new SelectList(Model.HAZID_Risk_Probability_Types, "Id", Resources.Resource.HAZID_Column_Select), Resources.Resource.HAZID_Risk_Probability_Id_Select, new { htmlAttributes = new { @class = "form-control", id = "HAZID_Form_HAZID_Risk_Probability_Id" } })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.HAZID_Form.HAZID_Control_Description, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-4">
@Html.EditorFor(model => model.HAZID_Form.HAZID_Control_Description, new { htmlAttributes = new { @class = "form-control", @style = "width:100% !important; min-width:600px; height:100px" } })
@Html.ValidationMessageFor(model => model.HAZID_Form.HAZID_Control_Description, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.HAZID_Form.HAZID_Person_Responsible_Name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.HAZID_Form.HAZID_Person_Responsible_Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.HAZID_Form.HAZID_Person_Responsible_Name, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.HAZID_Form.HAZID_Applcability_To_Others_Flag, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.CheckBoxFor(model => model.HAZID_Form.HAZID_Applcability_To_Others_Flag)
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-outline-primary" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "ListReports")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
<script type="text/javascript">
$(function () {
$("#HAZID_Form_HAZID_Status_Id").select2();
$("#HAZID_Form_HAZID_Hazard_Type_Id").select2();
$("#HAZID_Form_HAZID_Risk_Severity_Id").select2();
$("#HAZID_Form_HAZID_Risk_Probability_Id").select2();
$('.select2-container').css("width", "100%");
});
</script>
}
My problem is when I click the save the data is there but after the commit and redirect the data was not saved to the database.
if someone can help me it would be very much appreciated.
|