Hai,
I am trying to save some data from multiple entities using MVC 4. I can fire the action but I am not getting the values of the controls.
Please Help....
Thanks in Advance....
Code:
Model Class:-
public partial class mang_emp
{
public virtual tblEmployeetarget tblEmpTarget { get; set; }
public virtual tblDepartment tblDepartment { get; set; }
public decimal DeptID { get; set; }
public Nullable<decimal> CompID { get; set; }
public string DeptName { get; set; }
public decimal EMPTARGET_ID { get; set; }
public Nullable<decimal> SURVEY_ID { get; set; }
public Nullable<decimal> EMP_NUMBER { get; set; }
public Nullable<double> TRG_SCORE { get; set; }
}
public partial class tblDepartment
{
public decimal DeptID { get; set; }
public Nullable<decimal> CompID { get; set; }
public string DeptName { get; set; }
public Nullable<decimal> NoEmployee { get; set; }
public Nullable<double> TrgScore { get; set; }
public virtual tblCompany tblCompany { get; set; }
public virtual tblCompany tblCompany1 { get; set; }
public virtual tblEmployeetarget tblEmpTarget { get; set; }
}
public partial class tblEmployeetarget
{
public decimal EMPTARGET_ID { get; set; }
public Nullable<decimal> COMP_ID { get; set; }
public Nullable<decimal> SURVEY_ID { get; set; }
public Nullable<decimal> DEPT_ID { get; set; }
public Nullable<decimal> EMP_NUMBER { get; set; }
public Nullable<double> TRG_SCORE { get; set; }
public virtual tblDepartment tblDepartment { get; set; }
}
Controller:
-
[HttpPost]
public ActionResult Load(mang_emp emp)
{
tblEmployeetarget tblemployeetarget = new tblEmployeetarget()
{
COMP_ID = emp.CompID,
DEPT_ID = emp.DeptID,
EMP_NUMBER = emp.EMP_NUMBER,
EMPTARGET_ID = emp.EMPTARGET_ID,
SURVEY_ID = emp.SURVEY_ID,
TRG_SCORE = emp.TRG_SCORE
};
if (ModelState.IsValid)
{
db.tblEmployeetargets.Add(tblemployeetarget);
db.SaveChanges();
return RedirectToAction("Load");
}
return View(tblemployeetarget);
}
View:-
@model MVC_AlBatha.Models.mang_emp
@{
ViewBag.Title = "Manage Employee Number";
}
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<div class="editor-label">
@Html.Label("Select Company")
</div>
<div class="editor-field">
@Html.DropDownList("COMP_ID", "--Select--")
@Html.ValidationMessageFor(model => model.CompID)
</div>
<div class="editor-label">
@Html.Label("Select Survey ")
</div>
<div class="editor-field">
@Html.DropDownList("SURVEY_ID", "--Select--")
@Html.ValidationMessageFor(model => model.SURVEY_ID)
</div>
<table id="tbl">
</table>
if (ViewBag.flag != "0")
{
<table>
<tr>
<th>
Department Name
</th>
<th>
Number of Employees
</th>
<th>
Target Score
</th>
</tr>
@foreach (var item in ViewData["lis_dept"] as List<MVC_AlBatha.Models.mang_emp>)
{
<tr>
<td>
@Html.DisplayFor(modelItem=>item.DeptName)
</td>
<td>
@Html.TextBoxFor(modelItem=>item.EMP_NUMBER)
@Html.ValidationMessageFor(modelItem=>item.EMP_NUMBER)
</td>
<td>
@Html.TextBoxFor(modelItem=>item.TRG_SCORE)
@Html.ValidationMessageFor(modelItem=>item.TRG_SCORE)
</td>
</tr>
}
</table>
<p>
<input type="submit" value="Create" />
</p>
}
}