Entity Framework : Create, Edit, Update, Delete Operations with WCF using CODE FIRST method in MVC 4














































Step-42: WelcomeController.cs

using System.Web.Mvc;
using Mvc_WcfTrainingApplication.Models;
using Mvc_WcfTrainingApplication.MvcWcfServiceRefernce;
using Users = Mvc_WcfTrainingApplication.Models.Users;

namespace Mvc_WcfTrainingApplication.Controllers
{
    public class WelcomeController : Controller
    {
        //
        // GET: /Welcome/
        private readonly MvcWcfDbcontext _db = new MvcWcfDbcontext();
        private readonly Service1Client _mvcWcfServiceClient = new Service1Client();
        private string _operationName;
        private int _id;
        private string _firstName;
        private string _lastName;

        public ActionResult HomePageView()
        {
            return View("HomeView",_mvcWcfServiceClient.GetUserList());
        }

        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(Users users)
        {
            if (ModelState.IsValid)
            {
               //Use _db for creation DataBase using CodeFirst Method

                _db.Users.Add(users);
                _db.SaveChanges();
                
                return RedirectToAction("HomePageView");
            }
            return View("HomeView", _mvcWcfServiceClient.GetUserList());
        }

        public ActionResult Edit(int id=0)
        {
            var users = _db.Users.Find(id);
            if (users == null)
            {
                return HttpNotFound();
            }
            return View("Edit",users);
        }

        [HttpPost]
        public ActionResult Edit(Users users)
        {
            if (ModelState.IsValid)
            {
                _operationName = "Update";
                _id = users.Id;
                _firstName = users.FirstName;
                _lastName = users.LastName;
                _mvcWcfServiceClient.GetInsertUpdateDeleteValues(_operationName, _id, _firstName, _lastName);
                return RedirectToAction("HomePageView");
            }
            return View("HomeView", _mvcWcfServiceClient.GetUserList());
        }


        public ActionResult Delete(int id = 0)
        {
            var users = _db.Users.Find(id);
            if (users == null)
            {
                return HttpNotFound();
            }
            return View("Delete", users);
        }

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            _operationName = "Delete";
            _id = id;
            _firstName = string.Empty;
            _lastName = string.Empty;
            _mvcWcfServiceClient.GetInsertUpdateDeleteValues(_operationName, _id, _firstName, _lastName);
            return RedirectToAction("HomePageView");
        }

    }
}


Step-43: HomeView.cshtml

@model Mvc_WcfTrainingApplication.MvcWcfServiceRefernce.User[]

@{
    ViewBag.Title = "HomeView";
}

<h2>HomeView</h2>

@using (Html.BeginForm())
{
    <table>
    <thead>
        <tr>
            <td>ID</td>
            <td>First Name</td>
            <td>Last Name</td>
            <td>&nbsp;</td>
        </tr>
    </thead>

        <tbody>
            @foreach (var user in Model)
            {
                <tr>
                    <td>
                        @user.Id
                    </td>
                    <td>
                        @user.FirstName
                    </td>
                    <td>
                        @user.LastName
                    </td>
                    <td>
                         @Html.ActionLink("Edit", "Edit", "Welcome",new{id=user.Id},null)
                    </td>
                    <td>
                        @Html.ActionLink("Delete","Delete","Welcome",new{id=user.Id},null)
                    </td>
                </tr>
            }
        </tbody>

    <tfoot>
        <tr>
            <td>
                @Html.ActionLink("Create", "Create", "Welcome", null, null)
            </td>
        </tr>
    </tfoot>

</table>
}

Step 44: create.cshtml

@model Mvc_WcfTrainingApplication.Models.Users
@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>
@using (Html.BeginForm())
{
    <div>
        @Html.LabelFor(model => model.FirstName)
    </div>
    <div>
        @Html.EditorFor(model => model.FirstName)
    </div>

    <div>
        @Html.LabelFor(model => model.LastName)
    </div>
    <div>
        @Html.EditorFor(model => model.LastName)
    </div>

    <p>
        <input type="submit" value="Create" />
    </p>
}

<div>
    @Html.ActionLink("Back to Home Page", "HomePageView")
</div>


Step 45: Edit.cshtml

@model Mvc_WcfTrainingApplication.Models.Users

@{
    ViewBag.Title = "Edit";
}

<h2>Edit</h2>

@using (Html.BeginForm())
{

    @Html.HiddenFor(model => model.Id)

    <div>
        @Html.LabelFor(model => model.FirstName)
    </div>
    <div>
        @Html.EditorFor(model => model.FirstName)
    </div>

    <div>
        @Html.LabelFor(model => model.LastName)
    </div>
    <div>
        @Html.EditorFor(model => model.LastName)
    </div>

    <p>
        <input type="submit" value="Save" />
    </p>
    
}

<div>
    @Html.ActionLink("Back to Home Page", "HomePageView")
</div>


Step 46: Delete.cshtml

@model Mvc_WcfTrainingApplication.Models.Users
@{
    ViewBag.Title = "Delete";
}

<h2>Delete</h2>


<h3>Are you sure you want to delete this?</h3>

    <div >
         @Html.DisplayNameFor(model => model.FirstName)
    </div>
    <div >
        @Html.DisplayFor(model => model.FirstName)
    </div>

    <div >
         @Html.DisplayNameFor(model => model.LastName)
    </div>
    <div >
        @Html.DisplayFor(model => model.LastName)
    </div>

@using (Html.BeginForm()) {
    <p>
        <input type="submit" value="Delete" /> |
        @Html.ActionLink("Back to Home Page", "HomePageView")
    </p>
}

















Comments