Thursday, March 3, 2016

FamilyTree Application Using asp.net (MVC5)

          Easiest way to design a simple application to  learn - MVC5
Step:1
The execution goes to the global.asax page, invokes all the class files in the app_start folders where all the files are static.

Step:2
 The route.config file gets invoked and the path is decided, the default name of each routes defined should be different.

 routes.MapRoute(
          name: "Default1",
          url: "{controller}/{action}/{id}",
          defaults: new { controller = "Authenticate", action = "SignIn", id = UrlParameter.Optional }
      );

            routes.MapRoute(
              name: "Default",
              url: "{controller}/{action}/{id}",
              defaults: new { controller = "Family", action = "Index", id = UrlParameter.Optional }
          ).DataTokens = new RouteValueDictionary(new { area = "FamilyTree" });

         
    The first route invokes the signIn action specified in the Authentication controller.
    The second route specifies the Index action in Family controller where the controller is present inside the area.

Step:3  (Performing SignIn Authenticate the User custom)
The route specified url gets invoked.controller = "Authenticate", action = "SignIn" gets invoked.

Description:
         
 Controller:Authenticate
 Action    :SignIn
 ViewFolderName: SignIn
 ViewName  :SignIn.cshtml
1. For each controller name a folder is created in the view, and this is the user rendering page, where all csharp and html helper code designs are present.
          Controller:Authenticate
          Action    :SignIn

step A)  [HttpGet]
public ActionResult SignIn()
        {
            return View();
        }

this is the http get code where the page renders with the view design in the SignIn View

View Code:
 A view model is created in the model folder to pass the data from controller to view and view to controller.
 here we created a signInViewModel where we specified the validations using data annotations.

   Code:
public class SignInViewModel
    {
        [Required(ErrorMessage = "Please Provide Username", AllowEmptyStrings = false)]
        public string UserName { get; set; }

        [Required(ErrorMessage = "Please provide password", AllowEmptyStrings = false)]
        [DataType(DataType.Password)]
        public string Password { get; set; }
    }

Now import the model in the view and write the html,c sharp code

@model Farabiimvc.Models.SignInViewModel

@{
    ViewBag.Title = "Sign In";
    Layout = null;
}
<html>
<head>
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/jquery")

    @Scripts.Render("~/bundles/bootstrap")

</head>
<body style="background-image:url('http://www.hd-wallpapersdownload.com/upload/bulk-upload/cute-hd-wallpapers-pictures-gallery.jpg')">
    <div class="container" >
        <div class="row" style="margin-top:200px;">

            <div class="col-lg-offset-4 col-lg-4 col-md-4">
                <div class="panel panel-primary">
                    <div class="panel-heading text-center">
                        User Logon
                    </div>
                    <div class="panel-body">

                        @using (Html.BeginForm("SignIn", "Authenticate", FormMethod.Post, new { id="loginform" }))
                        {
                            @Html.AntiForgeryToken()
                   
                            <fieldset>
                                <div class="form-group">
                                    <div class="editor-label">
                                        @Html.LabelFor(model => model.UserName, new { @class = "control-label" })
                                    </div>
                                    <div class="editor-field">
                                        @Html.TextBoxFor(model => model.UserName, new { @class = "form-control" })

                                    


                                        @Html.ValidationMessageFor(model => model.UserName)
                                    </div>
                                </div>
                                <div class="form-group">
                                    <div class="editor-label">
                                        @Html.LabelFor(model => model.Password, new { @class = "control-label" })
                                    </div>
                                    <div class="editor-field">
                                        @Html.TextBoxFor(model => model.Password, new { @class = "form-control" })
                                        @Html.ValidationMessageFor(model => model.Password)
                                    </div>
                                </div>

                                <div class="form-group">
                                    <p class="col-lg-pull-2">
                                        <input type="submit" value="SignIn" id="submit" class="btn btn-primary"  />

                                    </p>
                                    <p class="pull-left">
                                        @Html.ActionLink("Forgot your password?", "ForgotPassword", "Authenticate", null, new { @class = "btn btn-default" })
                                        @Html.ActionLink("Register new user?", "RegisterUser", "Authenticate", null, new { @class = "btn btn-default" })
                                       
                                    </p>
                                </div>

                                <span>@Html.ValidationMessage("Error")</span>

                            </fieldset>

                        }
                    </div>
                </div>
            </div>

        </div>

    </div>
    @Scripts.Render("~/bundles/jqueryval")
 
</body>
</html>


1. Here when the user types the UserName and Password and clicks the submit button the post action gets called
2. code in post gets executed.If the user password and name is not empty or null then the name and password is checked in the database,using linq
3. if user is true or present.then the valid user is send to the "Index" action in the default "Home" controller.
3. else the user is not in db so we should restore all his details which he had already entered in this page so we return (entry)the user entered name and password.
          ie)  return View(entry);


         [HttpPost](Authenticate)
        public ActionResult SignIn(SignInViewModel entry)
        {
            if (!string.IsNullOrEmpty(entry.UserName) && !string.IsNullOrEmpty(entry.Password))
            {
                var searchUser = db.Users.Where(x => x.Password.Equals(entry.Password) && x.UserName.Equals(entry.UserName)).Any();
                if (searchUser == true)
                {

                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    ModelState.AddModelError("Error", "invalid username or password");

                }
            }
            return View(entry);

        }


Step:4 (Using Switch statment to switch to the user selected radiobutton Process)
So when the user is valid thats the point 2,3 happens then the
        Controller: "Home"
Action     :"Index"


public ActionResult Index()
        {
           
            return View();
        }
At first the page renders.
the view is displayed to user.

@{
    ViewBag.Title = "Home Page";
}

<h2>Welcome to Denishas Farab Technology the first and worst company in the world!!!!</h2>
<div class="row">
    <h3>What do you like to do</h3>
    @using (Html.BeginForm("Index", "Home", FormMethod.Post))
    {
    <ul class="list-group">

        <li class="list-group-item">
            @Html.RadioButton("SelectedOption","1", new {@class = "radio-inline" })
            1. Family Tree - A page wher you can manage your family details.

        </l
        <li  class="list-group-item">
            @Html.RadioButton("SelectedOption", "2")
            2.College Registration Number Splitter.
        </li>
        <li  class="list-group-item">
            @Html.RadioButton("SelectedOption", "3")
            3. BMI Calculator.
        </li>
        <li class="list-group-item" >
            @Html.RadioButton("SelectedOption", "4")
            4. Age Calculator

        </li>
     
    </ul>
 
    <input type="submit" name="Proceeed" class="btn btn-lg btn-success" value="Proceed" />
    }
</div>

Note here we didnt created any Viewmodel we used the default home controller and index action in the controller.
When the user selects an option from the radio button and presses the submit button in the form Userinterface.
then the post action gets called.

 Code:
      [HttpPost]
        public ActionResult Index(FormCollection form)
        {
            string SelectedValue = form["SelectedOption"];
            switch (SelectedValue)
            {
                case "1":
                    return RedirectToAction("Index", "Family", new {area = "FamilyTree" });
                    break;
                return View();
        }
        }

We have specified the code when the user selected value is the "SelectedOption","1" then we transfer the page to "Index" action in "Family" controller.
 ie)  case "1":
                    return RedirectToAction("Index", "Family", new {area = "FamilyTree" });


At first the get action calls and the page renders.
now the code is,
Get Code:          
             public ActionResult Index()
        {

              return View();
        }


View Code:
     
 @{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<h2>Welcome To Family Tree Page</h2>
<div class="row">
   @Html.ActionLink( "Do you wish to Retrieve existing family ?","ListFamily")
</div>
<div class="row">
    @Html.ActionLink("Add a new family ?", "AddFamily");
</div>


 Step:5 (List all families SelectAll ->readOperation)
Note : this is also a default page where no view model is specified.

When the user click the first link the action "ListFamily" is rendered with the view.
When the user click the second link the action "AddFamily" is rendered with the view.

User clicks the first link:
The get action gets called,
      Code:
     public ActionResult Listfamily(int id = 0)
        {

            List<FamilyIndex> family = db.FamilyIndexes.ToList();
            ViewBag.families = family;

            if (id > 0)
                ViewBag.familymembers = db.FamilyMembers.Where(f => f.FamilyID.Equals(id)).ToList();
            return View();
        }

list all the family in the familyindex domain table and send the family variable to the view. so that the table is done and the looping can be done.
and if the id is greater than zero then a familymember can be searched.when the get method calls ,the id is always zero , and so the id>0 function will not executes.

Domain Model :
Code:

      public class FamilyIndex
    {
        public FamilyIndex()
        {
            this.CreatedDate = DateTime.Now;
        }
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int FamilyID { get; set; }
        public string FamilyName { get; set; }
        public int TotalMembers { get; set; }
        public int TotalKids { get; set; }
        public DateTime CreatedDate { get; set; }
        public virtual ICollection<FamilyMember> FamilyMembers { get; set; }


    }

View Code:
   

@model Farabiimvc.DataLayer.BusinessObjects.FamilyIndex

<br />
<div class="container">
    @using (Html.BeginForm("ListFamily", "Family", FormMethod.Post, new { @class = "form-inline" }))
    {

        <div class="form-group">


            @Html.TextBoxFor(m => m.FamilyName, new { @class = "form-control", placeholder = "Family Name" })
         
        </div>

            <div class="form-group">
                <input type="submit" name="Search" class="btn btn-success" value="Search" />

            </div>
    }
    @Html.ActionLink("Add New Family", "AddFamily", null, new { @class = "btn btn-primary" })

</div>

<table class="table table-responsive">
    <tr>
        <th>
            @Html.DisplayNameFor(m => m.FamilyID)
        </th>
        <th>
            @Html.DisplayNameFor(m => m.FamilyName)
        </th>
        <th>
            @Html.DisplayNameFor(m => m.TotalMembers)
        </th>
        <th>
            @Html.DisplayNameFor(m => m.TotalKids)
        </th>
        <th>
            @Html.DisplayNameFor(m => m.CreatedDate)
        </th>
        <th>
            #
        </th>
    </tr>
    <tbody>
        @foreach (var family in ViewBag.families)
            {
            <tr>
                <td>
                    @family.FamilyID
                </td>
                <td>
                    @family.FamilyName
                </td>
                <td>
                    @family.TotalMembers
                </td>
                <td>
                    @family.TotalKids
                </td>
                <td>
                    @family.CreatedDate
                </td>
                <td>
                    @Html.ActionLink("Select", "ListFamily", new { id = @family.FamilyID })
                </td>
                <td>
                    @Html.ActionLink("Edit", "EditFamily", new { id = @family.FamilyID })
                </td>
                <td>
                    @Html.ActionLink("AddMembers", "AddFamilyMembers", new { id = @family.FamilyID })
                </td>
            </tr>
        }
    </tbody>

</table>

here a search button, a addnewfamily button and a table to display all the families is present.
when user clicks on the  "Select" action link "ListFamily" action link is called from family table.
when user clicks on the  "Edit" action link "EditFamily" action link is called from family table.
when user clicks on the "AddMembers"action link "AddFamilyMembers" action link is called from family table.
For all these to happen we should pass the FamilyID from the FamilyIndex table.

When the user types the name and clicks on the search button the httpPost action of list is invoked.

step:6 (SearchOperation using linq)
  code:
 
    [HttpPost]
        public ActionResult ListFamily(string FamilyName)
        {

            if (!string.IsNullOrEmpty(FamilyName))
            {
                var familysearch = db.FamilyIndexes.Where(s => s.FamilyName.Contains(FamilyName)).ToList();
                ViewBag.families = familysearch;
            }

            return View();
        }

here HttpGet gets called,when  "Select" action link  clicked to select from database.

here we pass the FamilyName in FamilyIndex table as the parameter with string datatype.
All the families belonging to the user typed FamilyName is displayed in the table.

when user clicks on the  "Select" action link "ListFamily" action link is called from family table.

FamilyID is passed from the family table to here as parameter.the selected families id is passed.
And now here the id>0 function becomes true and the id of the particular family invokes the FamilyMembers in the particular id and displays in the table
as send from the controller to view as
             ie)ViewBag.families = familysearch;

here the ,
 Viewcode is,

       @if (ViewBag.familymembers != null)
{


    <table class="table table-responsive">
        <tr>
            <th>
                Name
            </th>
            <th>
                Age
            </th>
            <th>
                DateOfBirth
            </th>
            <th>

                Gender
            </th>
            <th>
                Relation
            </th>
            <th>
                FamilyID
            </th>
            <th>
                #
            </th>
        <tbody>

            @foreach (var members in ViewBag.familymembers)
                {
                <tr>
                    <td>
                        @members.Name
                    </td>
                    <td>
                        @members.Age
                    </td>
                    <td>
                        @members.DateOfBirth
                    </td>
                    <td>
                        @members.Gender
                    </td>
                    <td>
                        @members.Relation
                    </td>
                    <td>
                        @members.FamilyID
                    </td>
                    <td>
                    <td>
                        @Html.ActionLink("EditFamilyMembers", "EditFamilyMembers", new {id = @members.ID})
                    </td>
                </tr>
            }
        </tbody>

    </table>
}


so atlast the list view contains  a search button, a addnewfamily button and a table to display all the families is present,
a table to display the family members when the user clicks on the select and selects a particular family with members thats id>0.
here the details of the famlily members is diplayed.
                          ie)http://localhost:8591/FamilyTree/Family/ListFamily/3

when user clicks on the  "Edit" action link "EditFamily" action link is called from family table.

HttpGet gets called,

Step:7 (EditOperaion for Family)
  Code:
       public ActionResult EditFamily(int id = 0)
        {
            FamilyIndex entry = db.FamilyIndexes.Find(id);
            var family = AutoMapper.Mapper.Map<FamilyViewModel>(entry);

            if (entry == null)
            {
                return HttpNotFound();
            }

            return View(family);
        }


Find the families with the id and stored in the entry and the entry is actually the domain model
so it is automapped and converted into viewmodel becz the details of
particular member with id >0  must remain here so to pass to view the domain model object entry is converted into view model.

When the user edits the member details and clicks the save button HttpPost action method gets called.
when posting the model object should be passed to the view.
   Code:
       ie)model object is passed as parameter.
 
public ActionResult EditFamily(FamilyViewModel entry)
        {
            if (ModelState.IsValid)
            {
                var family = AutoMapper.Mapper.Map<FamilyIndex>(entry);

                db.Entry(family).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("ListFamily");
            }
            return View(entry);

        }

Step :8(InsertOperation for FamilyMembers)-> adding a member in family

when user clicks on the "AddMembers"action link "AddFamilyMembers" action link is called from family table.
Get Code:

       public ActionResult AddFamilyMembers(int id = 0)
        {
            //Country country = new Country();
            var Countries = db.Countries.ToList();

            ViewBag.Countries = new SelectList(Countries, "CountryID", "CountryName");


            if (id > 0)
            {
                ViewBag.FamilyMembers = db.FamilyMembers.Where(p => p.FamilyID.Equals(id)).Include("Country").ToList();
            }

            return View();    

        }


View Code:

@model Farabiimvc.Areas.FamilyTree.Models.FamilyMemberViewModel

@{
    ViewBag.Title = "AddFamilyMembers";
}

<h3>Add Family Members</h3>
<div class="row">
    <div class="col-md-6">


        @using (Html.BeginForm("AddFamilyMembers", "Family", FormMethod.Post, new { @class = "form-Horizontal" }))
        {
            <div class="row" style="margin:5px 5px 5px 5px;">
                <div class="form-group">
                    @Html.LabelFor(m => m.Name, new { @class = "control-label col-lg-4" })
                    <div class="col-lg-6 input-group">
                        @Html.TextBoxFor(m => m.Name, new { @class = "form-control" })
                        @Html.ValidationMessageFor(m => m.Name, "", new { @class = "text-danger" })
                        &nbsp &nbsp
                    </div>
                </div>

                <div class="form-group ">
                    @Html.LabelFor(m => m.Age, new { @class = "control-label col-lg-4" })
                    <div class="col-lg-6 input-group">
                        @Html.TextBoxFor(m => m.Age, new { @class = "form-control" })
                        @Html.ValidationMessageFor(m => m.Age, "", new { @class = "text-danger" })
                        &nbsp &nbsp
                    </div>
                </div>

                <div class="form-group">
                    @Html.LabelFor(m => m.DateOfBirth, new { @class = "control-label col-lg-4" })
                    <div class="col-lg-6 input-group">
                        @Html.TextBoxFor(m => m.DateOfBirth, new { @class = "form-control" })
                        @Html.ValidationMessageFor(m => m.DateOfBirth)
                        &nbsp &nbsp
                    </div>
                </div>
                <div class="form-group">
                    @Html.LabelFor(m => m.Gender, new { @class = "control-label col-lg-4" })
                    <div class="col-lg-6 input-group">
                        @Html.TextBoxFor(m => m.Gender, new { @class = "form-control" })
                        @Html.ValidationMessageFor(m => m.Gender)
                        &nbsp &nbsp
                    </div>
                </div>
                <div class="form-group">
                    @Html.LabelFor(m => m.Relation, new { @class = "control-label col-lg-4" })
                    <div class="col-lg-6 input-group">
                        @Html.TextBoxFor(m => m.Relation, new { @class = "form-control" })
                        @Html.ValidationMessageFor(m => m.Relation)
                        &nbsp &nbsp
                    </div>
                </div>

                <div class="form-group">
                    @Html.LabelFor(m => m.CountryID, new { @class = "control-label col-lg-4" })
                    <div class="col-lg-6 input-group">

                        @Html.DropDownList("CountryID", (SelectList)ViewBag.Countries, "Select Country", new { @class = "form-control" })
                        &nbsp &nbsp

                        @Html.ValidationMessageFor(m => m.CountryID)

                    </div>
                </div>
            </div>
            <div class="row text-center">
                <p>
                    <input type="submit" value="AddFamilyMembers" class="btn btn-danger" />
                </p>
            </div>
            <div class="row">
                @if (ViewBag.Result != null)
                {
                    <div class="alert alert-success"><span>@ViewBag.Result</span></div>
                }
                @if (ViewData.ModelState["Error"] != null)
                {

                    <div class="alert alert-danger"><span>@Html.ValidationMessage("Error")</span></div>
                }
            </div>
            @Html.ActionLink("Back to list", "ListFamily")
        }
    </div>
    @if (ViewBag.FamilyMembers != null)
    {
        <div class="col-md-4">
            <table class="table table-bordered">
                <thead>
                <td>ID</td>
                <td>Name</td>
                <td>Gender</td>
                <td>Countries</td>
                <td>Edit</td>
                </thead>
                <tbody>
                    @foreach (var member in ViewBag.FamilyMembers)
                {
                        <tr>
                            <td>@member.ID</td>
                            <td>@member.Name</td>
                            <td>@member.Gender</td>
                            <td>@member.Country.CountryName</td>
                            <td>@Html.ActionLink("Edit Family Members", "EditFamilyMembers")</td>
                        </tr>
                    }
                </tbody>

            </table>
        </div>

    }
</div>
@section Scripts {
    @*//jquery validation plugin,jquery style css, jquery ui*@
    @Scripts.Render("~/bundles/jqueryval")

    @Styles.Render("~/Content/jquery-ui")

    @Scripts.Render("~/bundles/jquery-ui")
    <script>
        $(function () {
            $("#DateOfBirth").datepicker(
                {
                    dateFormat: "yy-mm-dd"
                }
                    );

        });

    </script>

}

FamilyMemberViewModel

Code:
public class FamilyMemberViewModel
    {


        [Display(Name = "FamilyID")]
        public int FamilyID { get; set; }


        [Required]
        [Display(Name = "Full Name")]
        public string Name { get; set; }

        [Required]
        [Display(Name = "Relation")]
        public string Relation { get; set; }
        [Required]
        [Display(Name = "Age")]
        public int Age { get; set; }

        [Required]
        [Display(Name = "Date Of Birth")]
        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
        public DateTime DateOfBirth { get; set; }

        [Required]
        [Display(Name = "Gender")]
        public string Gender { get; set; }

       
        private readonly List<SelectListItem> _countries;

        [Required]
        [Display(Name = "Country")]
        public int CountryID { get; set; }

        public IEnumerable<SelectListItem> Countries
        {
            get { return new SelectList(_countries, "CountryID", "Name"); }
        }
    }


   (InsertOperation for AddFamilyMembers) ->Post

[HttpPost]
        public ActionResult AddFamilyMembers(FamilyMemberViewModel family, int id)
        {
            if (ModelState.IsValid)
            {

                var familymember = AutoMapper.Mapper.Map<FamilyMember>(family);
                //linking the reference of master tabel
                familymember.FamilyID = id;

                db.FamilyMembers.Add(familymember);

                db.SaveChanges();
                ModelState.Clear();
                var Countries = db.Countries.ToList();

                ViewBag.Countries = GetCountriesfordropdown();
            }

            return View();

        }

        public SelectList GetCountriesfordropdown()
        {
            List<Country> Countries = db.Countries.ToList();
            return new SelectList(Countries, "CountryID", "CountryName");
        }


EditFamilyMembers:(Edit FamilyMember)
Code:
      public ActionResult EditFamilyMembers(int id)
        {
         
            FamilyMember familymember = db.FamilyMembers.Find(id);
            var member = AutoMapper.Mapper.Map<FamilyMemberViewModel>(familymember);

            ViewBag.Countries = GetCountriesfordropdown();

            if (member == null)
            {
                return HttpNotFound();
            }
            return View(member);
        }

View code:

@model Farabiimvc.Areas.FamilyTree.Models.FamilyMemberViewModel
@{
    ViewBag.Title = "EditFamilyMembers";
}

<h2>EditFamilyMembers</h2>

@using (Html.BeginForm("EditFamilyMembers", "Family",FormMethod.Post, new { @class = "form-Horizontal" }))
{
 
    <div class="row" style="margin:5px 5px 5px 5px;">
     
        <div class="form-group">
            @Html.LabelFor(m => m.DateOfBirth, new { @class = "control-label col-lg-4" })
            <div class="col-lg-6 input-group">
                @Html.TextBoxFor(m => m.DateOfBirth, new { @class = "form-control" })
                @Html.ValidationMessageFor(m => m.DateOfBirth)
                &nbsp &nbsp
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(m => m.Gender, new { @class = "control-label col-lg-4" })
            <div class="col-lg-6 input-group">
                @Html.TextBoxFor(m => m.Gender, new { @class = "form-control" })
                @Html.ValidationMessageFor(m => m.Gender)
                &nbsp &nbsp
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(m => m.Relation, new { @class = "control-label col-lg-4" })
            <div class="col-lg-6 input-group">
                @Html.TextBoxFor(m => m.Relation, new { @class = "form-control" })
                @Html.ValidationMessageFor(m => m.Relation)
                &nbsp &nbsp
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(m => m.CountryID, new { @class = "control-label col-lg-4" })
            <div class="col-lg-6 input-group">

                @Html.DropDownList("CountryID",  (SelectList)ViewBag.Countries, "Select Country", new { @class = "form-control"})

                &nbsp &nbsp

                @Html.ValidationMessageFor(m => m.CountryID)

            </div>
            @if( ViewBag.editmembers>0)
            {
            <div>
                @Html.ActionLink("AddFamilyMembers", "Family", new { id = Model.FamilyID})  
            </div>
            <div>
                <input type="submit" value="EditFamilyMembers" class="btn btn-danger" />
                @Html.ActionLink("Save", "Family", new { id = Model.FamilyID })
            </div>

            }

        </div>
    </div>

}
Code :

   [HttpPost]
       
        public ActionResult EditFamilyMembers(FamilyMemberViewModel model, int id)
        {
            if (ModelState.IsValid)
            {
                var members = AutoMapper.Mapper.Map<FamilyMember>(model);
                db.Entry(members).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("ListFamily");


            }
            return View(model);
        }


Delete Code:(DeleteFamily)
[HttpGet]
        public ActionResult DeleteFamily(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }

            FamilyMember family = db.FamilyMembers.Find(id);

            if (family == null)
            {
                return HttpNotFound();
            }

            db.FamilyMembers.Remove(family);
            db.SaveChanges();
            ViewBag.Result = "Deleted Sucessfully";
            return RedirectToAction("ListFamily");

        }

During delete we use the domain model for view.

@model Farabiimvc.DataLayer.BusinessObjects.Family
@{
    ViewBag.Title = "DeleteFamily";
}

<h2>DeleteFamily</h2>

@using (Html.BeginForm("DeleteConfirmed", "Family", FormMethod.Post))
{
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Delete Family</legend>
        <div class="row">
            <div class="form-group">
                @Html.LabelFor(m => m.Name, new { @class = "Control-label col-lg-4" })
                <div class="col-lg-6 input-group">
                    @Html.TextBoxFor(m => m.Name, new { @class = "form-control" })
                    @Html.ValidationMessageFor(m => m.Name, "", new { @class = "text-danger" })
                </div>
            </div>
            &nbsp &nbsp
            <div class="form-group">
                @Html.LabelFor(m => m.Age, new { @class = "Control-label col-lg-4" })
                <div class="col-lg-6 input-group">
                    @Html.TextBoxFor(m => m.Age, new { @class = "form-control" })
                    @Html.ValidationMessageFor(m => m.Age, "", new { @class = "text-danger" })
                </div>
            </div>
            &nbsp &nbsp
            <div class="form-group">
                @Html.LabelFor(m => m.DateOfBirth, new { @class = "Control-label col-lg-4" })
                <div class="col-lg-6 input-group">
                    @Html.TextBoxFor(m => m.DateOfBirth, new { @class = "form-control" })
                    @Html.ValidationMessageFor(m => m.DateOfBirth, "", new { @class = "text-danger" })
                </div>
            </div>
            &nbsp &nbsp
            <div class="form-group">
                @Html.LabelFor(m => m.Gender, new { @class = "control-label col-lg-4" })
                <div class="col-lg-6 input-group">
                    @Html.TextBoxFor(m => m.Gender, new { @class = "form-control" })
                    @Html.ValidationMessageFor(m => m.Gender,"", new { @class = "text-danger"})
                    &nbsp &nbsp
                </div>
            </div>
            &nbsp &nbsp
            <div class="form-group">
                @Html.LabelFor(m => m.Relation, new { @class = "control-label col-lg-4" })
                <div class="col-lg-6 input-group">
                    @Html.TextBoxFor(m => m.Relation, new { @class = "form-control" })
                    @Html.ValidationMessageFor(m => m.Relation, "", new { @class = "text-danger" })
                </div>
            </div>
            &nbsp &nbsp
        </div>
        <div class="row">
            @if (ViewBag.Result == null)
            {
                <div class="alert alert-success"><span>@ViewBag.Result</span></div>
            }
        </div>

        <div>@Html.ActionLink("Back To List", "EditFamily", "Family")</div>


    </fieldset>
 
}

    @section Scripts {
        @Scripts.Render("~/bundles/jqueryval")


    }
Include the automapper from the managenuget package solution,

Include an Automapper class in app_start folder and add  [using AutoMapper;]
specify a static class here in the Automapperconfig.cs ,to call this from global.asax

public static void  RegisterMappings()
        {
           
            Mapper.CreateMap<FamilyMember, FamilyMemberViewModel>().ReverseMap();
            Mapper.CreateMap<FamilyIndex,FamilyViewModel>().ReverseMap();
        }

goto Global.asax and call the automapper class here,

AutoMapperConfig.RegisterMappings();


 Thank YoU!...



 

No comments:

Post a Comment