vendredi 6 novembre 2015

Insert & Update in Asp.Net MVC3

I can't create and update the information by this way: Using Initializer, and Combing two models classes to get the Index View,I have two tables EmployeeInformation & DesignationHierarchy as: CREATE TABLE [dbo].[DesignationHierarchy]( [Id] [numeric](18, 0) IDENTITY(1,1) NOT NULL, [EmployeeId] [numeric](18, 0) NULL, [LineManagerId][numeric](18, 0) NULL ) & CREATE TABLE [dbo].[EmployeeInformation]( [Id] [numeric](18, 0) IDENTITY(1,1) NOT NULL, [Name] [nvarchar](255) NULL ) and ModelClasses as: ` public class DesignationHierarchy { public decimal Id { get; set; } public decimal EmployeeId { get; set; } public decimal LineManagerId { get; set; }

}`

`public class EmployeeInformation { public decimal Id { get; set; } public string Name { get; set; }

}`

and public class EmployeeInformationHierarchy { public EmployeeInformation EmployeeInformation { get; set; } public DesignationHierarchy DesignationHierarchy { get; set; } } and the methods to Create: ` public ActionResult Create() {

        ViewBag.EmployeeName = new SelectList(db.EmployeeInformation.ToList(),"Id","Name");

        return View();
    } 
   [HttpPost]
    public ActionResult Create(DesignationHierarchy designationHierarchy)
    {
        if (ModelState.IsValid)
        {
            db.DesignationHierarchy.Add(designationHierarchy);
            db.SaveChanges();
            return RedirectToAction("Index");  
        }
        return View(designationHierarchy);
    }`

and edit: public ActionResult Edit(decimal id) { DesignationHierarchy designationHierarchy = db.DesignationHierarchy.Find(id); ViewBag.EmployeeName = new SelectList(db.EmployeeInformation.ToList(), "Id", "Name",designationHierarchy); return View(designationHierarchy); } [HttpPost] public ActionResult Edit(DesignationHierarchy designationHierarchy) { if (ModelState.IsValid) { db.Entry(designationHierarchy).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } return View(designationHierarchy); } and for index: ` public ViewResult Index() {

        var designationHierarchy = from d in db.EmployeeInformation


                                   join r in db.DesignationHierarchy on d.Id equals r.LineManagerId


                                   select new EmployeeInformationHierarchy { EmployeeInformation = d, DesignationHierarchy = r };

        return View(designationHierarchy);
    }`

and Views: ` @Html.HiddenFor(model => model.Id) @Html.LabelFor(model => model.EmployeeId,"Name") @Html.DropDownListFor(m => m.EmployeeId, (SelectList)ViewBag.EmployeeName, "--Select Name--") @Html.ValidationMessageFor(model => model.EmployeeId)

    <div class="editor-label left_align">
        @Html.LabelFor(model => model.LineManagerId,"Line Manager")
    </div>
    <div class="editor-field left_right_padding">
   @Html.DropDownListFor(m => m.LineManagerId, (SelectList)ViewBag.EmployeeName, "--Select Name--")
        @Html.ValidationMessageFor(model => model.LineManagerId)
    </div>`

and view for index: <td> @Html.DisplayFor(modelItem => item.EmployeeInformation.Name) </td> <td> @Html.DisplayFor(modelItem => item.EmployeeInformation.Name) </td> <td> @Html.ActionLink("Edit", "Edit", new { id=item.DesignationHierarchy.Id }) | @Html.ActionLink("Delete", "Delete", new { id=item.DesignationHierarchy.Id }) </td>

Aucun commentaire:

Enregistrer un commentaire