Click here to Skip to main content
15,893,487 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:-
C#
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; }
    }


C#
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; }

    }


C#
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:

-
C#
[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:-
HTML
@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>
    }
}
Posted
Updated 22-Jul-15 18:43pm
v3
Comments
F-ES Sitecore 22-Jul-15 6:38am    
Go through some tutorials on mvc and data binding. Your html controls do not relate to your model. You have a single property for EMP_NUMBER etc yet you can have multiple controls on the form. The model needs to match your form and yours doesn't.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900