locked
how to insert foreign key empid in the table? RRS feed

  • Question

  • User1421057020 posted

    I am facing a referance issue

    empid is not insert in the table when employee generate the experiance then not display the experiance data on the index page?

    HomeController.cs

        public class HomeController : Controller
        {
            private readonly dbozeeEntities dbemp;
    
            public HomeController()
            {
                dbemp = new dbozeeEntities();
            }
    
            // GET: Home
            public ActionResult Index()
            {
                var list = dbemp.employees.ToList();
                return View(list);
            }
    
            public ActionResult Create()
            {
                return View();
            }
    
            [HttpPost]
            public ActionResult Create(employee emp)
            {
                var create = dbemp.employees.Add(emp);
                dbemp.SaveChanges();
                return RedirectToAction("Experiance");   //here going to experiance page
            }
    
            public ActionResult Experiance()
            {
                return View();
            }
    
            [HttpPost]
            public ActionResult Experiance(experiance exp,employee emps)
            {
                ViewData["empid"] = emps.empid;  
    
                var create = dbemp.experiances.Add(exp);
                dbemp.SaveChanges();
                return RedirectToAction("Index");
            }

    Index.cshtml

    @model IEnumerable<DemoOzeeTechno.Models.employee>
    @using DemoOzeeTechno.Models
    @{
        ViewBag.Title = "Index";
        IEnumerable<experiance> exp = ViewData["empid"] as IEnumerable<experiance>;
    }
    
    <h2>Index</h2>
    
    <p>
        @Html.ActionLink("Create New", "Create")
    </p>
    <table class="table">
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.empname)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.emailaddress)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.contactno)
            </th>
            <th></th>
        </tr>
    
    @foreach (var item in Model) {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.empname)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.emailaddress)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.contactno)
            </td>
            <td>
                @Html.ActionLink("Edit", "Edit", new { id=item.empid }) |
                @*@Html.ActionLink("Details", "Details", new { id=item.empid }) |*@
                @Html.ActionLink("Delete", "Delete", new { id=item.empid })
            </td>
        </tr>
    }
    
    </table>
    
    <table>
        <tr>
            <th>CompanyName</th>
            <th>YearOfExperiance</th>
        </tr>
    
        @if (ViewData["empid"] != null)
        {
            foreach (experiance ex in exp)
            {
                <tr>
                    <td>@ex.companyname</td>
                    <td>@ex.yearofexp</td>
                </tr>
            }
        }
    </table>

    Experiance.cshtml

    @model DemoOzeeTechno.Models.experiance
    
    @{
        ViewBag.Title = "Experiance";
    }
    
    <h2>Experiance</h2>
    
    
    @using (Html.BeginForm()) 
    {
        @Html.AntiForgeryToken()
    
        <div class="form-horizontal">
            <h4>experiance</h4>
            <hr />
            @Html.ValidationSummary(true, "", new { @class = "text-danger" })
            <div class="form-group">
                @Html.LabelFor(model => model.companyname, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.companyname, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.companyname, "", new { @class = "text-danger" })
                </div>
            </div>
    
            <div class="form-group">
                @Html.LabelFor(model => model.yearofexp, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.yearofexp, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.yearofexp, "", new { @class = "text-danger" })
                </div>
            </div>
    
            @*<div class="form-group">
                @Html.LabelFor(model => model.empid, "empid", htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.DropDownList("empid", null, htmlAttributes: new { @class = "form-control" })
                    @Html.ValidationMessageFor(model => model.empid, "", new { @class = "text-danger" })
                </div>
            </div>*@               //here empid is a comment code
    
            <div class="form-group">
                <div class="col-md-offset-2 col-md-10">
                    <input type="submit" value="Create" class="btn btn-default" />
                </div>
            </div>
        </div>
    }
    
    <div>
        @Html.ActionLink("Back to List", "Index")
    </div>
    
    <script src="~/Scripts/jquery-1.10.2.min.js"></script>
    <script src="~/Scripts/jquery.validate.min.js"></script>
    <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>

    Database Design:

    experiance data inserted in table but issue is Does not Displaying the experiance data in index page

    Watch Window:

    experiance data is inserted in the table but empid is not insert and experiance data is not displayed on the index page?

    Wednesday, June 10, 2020 11:59 AM

All replies

  • User1421057020 posted
            public ActionResult Experiance(employee emps)
            {
                ViewData["empid"] = emps.empid;
    
                return View();
            }

    //when I getting the inserted experiance data also getting the 0(emps.empid); when I am debugging

    Wednesday, June 10, 2020 12:31 PM
  • User475983607 posted

    This is a very common issue caused by the input element name not matching the input property name.  For model binding to work properly you must follow the naming convention.  The easiest way to check this simply reviewing the post parameters in the browsers dev tools to the action input parameters.  

    Your original post shows the empid input is commented and does not exist.  

    Wednesday, June 10, 2020 12:46 PM
  • User1421057020 posted

    Hello mgebhard

    I am not finding solution for this still empid give the zero

    ViewData["empid"] = emps.empid;  

    which Place I mistake still I am finding the solution?

    Wednesday, June 10, 2020 3:06 PM
  • User1421057020 posted

    issue is solved 

    Thursday, June 11, 2020 11:18 AM
  • User1686398519 posted

    Hi,  raju bhai

    • I am glad that you have solved your problem. You can give your solution.
    • This is my solution, if you are interested, you can refer to it.
      • I used "TempData" and Tuple to get empid.

    Controller

            public ActionResult Create()
            {
                return View();
            }
    
            [HttpPost]
            public ActionResult Create(employee emp)
            {
                var create = dbemp.employees.Add(emp);
                dbemp.SaveChanges();
                
                TempData["emp"] = emp;
                return RedirectToAction("Experiance");   //here going to experiance page
            }
    
            public ActionResult Experiance()
            {
                var emp = TempData["emp"] as employee;
                var exp = new experiance();
                var t = new Tuple<experiance, employee>(exp, emp);
                return View(t);
            }
            [HttpPost]
            public ActionResult Experiance(experiance Item1,employee Item2)
            {
                Item1.empid = Item2.empid;
                var create = dbemp.experiances.Add(Item1);
                dbemp.SaveChanges();
                return RedirectToAction("Index");
            }

    Experiance

    @using WebApplication3.Models
    @model Tuple<experiance,employee>
    @{
        ViewBag.Title = "Experiance";
    }
    <h2>Experiance</h2>
    @using (Html.BeginForm())
    {
        @Html.AntiForgeryToken()
    
        <div class="form-horizontal">
            <h4>experiance</h4>
            <hr />
            @Html.ValidationSummary(true, "", new { @class = "text-danger" })
            <div class="form-group">
                @Html.LabelFor(model => model.Item1.companyname, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.Item1.companyname, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.Item1.companyname, "", new { @class = "text-danger" })
                </div>
            </div>
    
            <div class="form-group">
                @Html.LabelFor(model => model.Item1.yearofexp, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.Item1.yearofexp, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.Item1.yearofexp, "", new { @class = "text-danger" })
                </div>
            </div>
            @Html.HiddenFor(model=>model.Item2.empid)           
            <div class="form-group">
                <div class="col-md-offset-2 col-md-10">
                    <input type="submit" value="Create" class="btn btn-default" />
                </div>
            </div>
        </div>
    }

    Here is the result.

     
    Best Regards,

    YihuiSun

    Friday, June 12, 2020 1:24 AM