Click here to Skip to main content
15,918,258 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

In my project I have to count the number of days between Given Start and End Date in Asp.Net MVC3.

In the data base fields are as above

leave_id int
leave_start_date DateTime
leave_end_date DateTime
leave_days int

In which start date and end date entered by user.
According to that i have to count the leave days
and then i have to save all these in database.

my .cs files are as below

LeaveSet.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;

namespace LeaveModule.Models
{
    public class LeaveSet
    {
        public int user_id { get; set; }
        public DateTime leave_start_date { get; set; }
        public DateTime leave_end_date { get; set; }
        public string leave_description { get; set; }
        public decimal leave_days { get; set; } 
    }
}



another file LeaveSetFetch.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ProjectManagementSystem.Models;

namespace LeaveModule.Models
{
    public class LeaveSetFetch
    {

        public static IList<LeaveSet> all()
        {
            IList<LeaveSet> result =
                (IList<LeaveSet>)HttpContext.Current.Session["leave"];

            if (result == null)
            {
                HttpContext.Current.Session["leave"] = result =
                    (from l in new ProjectManagementSystemEntities3().leave_master
                     select new LeaveSet
                     {
                         user_id = l.user_id,
                         leave_start_date = l.leave_start_date,
                         leave_end_date = l.leave_end_date,
                         leave_description = l.leave_description,
                         leave_days = l.leave_days,
                   

                     }).ToList();

            }
            return result;
        }
    }
}



Leavecontroller.cs file is below..

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ProjectManagementSystem.Models;
using Telerik.Web.Mvc;

namespace LeaveModule.Controllers
{
    public class LeaveController : Controller
    {
        //
        // GET: /Leave/
        
       public ProjectManagementSystemEntities3 pb = new ProjectManagementSystemEntities3();
  
        public ActionResult Index()
       {
          
           return View();
        }

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

        [HttpPost]
        public ActionResult LeaveSet(leave_master l)
        {
           
            if (ModelState.IsValid)
            {
                pb.leave_master.AddObject(l);
                pb.SaveChanges();
                return RedirectToAction("Index");
            }

            return RedirectToAction("Index");
        }

     
      

        public ActionResult LeaveShow()
        {
            var leave_master = pb.leave_master.Include("user_master");
            ViewBag.leave_id = new SelectList(pb.user_master, "user_id", "user_real_name");
            return View(leave_master.ToList());
        }

        [GridAction]
        public ActionResult _SelectBatchEditing()
        {
            return View(new GridModel(LeaveGridviewFetch.all()));
        }
    }
}



View file

LeaveSet.ascx(path View/Leave/LeaveSet.ascx)

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<LeaveModule.Models.LeaveSet>" %>

<script src="<%: Url.Content("~/Scripts/jquery-1.5.1.min.js") %>" type="text/javascript"></script>
<script src="<%: Url.Content("~/Scripts/jquery.validate.min.js") %>" type="text/javascript"></script>
<script src="<%: Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js") %>" type="text/javascript"></script>

<% using (Html.BeginForm("LeaveSet","Leave",FormMethod.Post)) { %>

    <%: Html.ValidationSummary(true) %>
    <fieldset>
        <legend>LeaveSet</legend>

        <div class="editor-label">
            <%: Html.LabelFor(model => model.user_id) %>
        </div>

        <div class="editor-field">
            <%: Html.EditorFor(model => model.user_id) %>
            <%: Html.ValidationMessageFor(model => model.user_id) %>
        </div>

        <div class="editor-label">
            <%: Html.LabelFor(model => model.leave_start_date) %>
        </div>

        <div class="editor-field">
            <%: Html.EditorFor(model => model.leave_start_date) %>
            <%: Html.ValidationMessageFor(model => model.leave_start_date) %>
        </div>

        <div class="editor-label">
            <%: Html.LabelFor(model => model.leave_end_date) %>
        </div>

        <div class="editor-field">
            <%: Html.EditorFor(model => model.leave_end_date) %>
            <%: Html.ValidationMessageFor(model => model.leave_end_date) %>
        </div>

        <div class="editor-label">
            <%: Html.LabelFor(model => model.leave_description) %>
        </div>

        <div class="editor-field">
            <%: Html.EditorFor(model => model.leave_description) %>
            <%: Html.ValidationMessageFor(model => model.leave_description) %>
        </div>

      

        

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

<div>
    <%: Html.ActionLink("Back to List", "Index") %>
</div>



Where ProjectManagementSystemEntities3 is an object of .edmx file which is created from database. and leave_master is my table..
So how to do these in MVC?
Posted
Updated 22-Mar-12 10:43am
v5

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ProjectManagementSystem.Models;

namespace LeaveModule.Models
{
    public class LeaveSetFetch
    {

        public static IList<LeaveSet> all()
        {
            IList<LeaveSet> result =
                (IList<LeaveSet>)HttpContext.Current.Session["leave"];

            if (result == null)
            {
                HttpContext.Current.Session["leave"] = result =
                    (from l in new ProjectManagementSystemEntities3().leave_master
                     select new LeaveSet
                     {
                         user_id = l.user_id,
                         leave_start_date = l.leave_start_date,
                         leave_end_date = l.leave_end_date,
                         leave_description = l.leave_description,
                         TimeSpan ts = leave_end_date.Subtract(leave_start_date);
                         leave_days = ts.Days;

                     }).ToList();

            }
            return result;
        }
    }
}
 
Share this answer
 
v2
Comments
disha desani 22-Mar-12 4:18am    
will you tell me in which file from above i have to use these code?
Sushil Mate 22-Mar-12 4:45am    
check my updated solution
disha desani 22-Mar-12 5:56am    
i tried that but it doesn't allow to use timespan in that place. giving an error.
Sushil Mate 22-Mar-12 6:33am    
what error?
Sushil Mate 22-Mar-12 6:35am    
ohh define Timespan ts initially.
hi try this one
C#
TimeSpan ts = DateTime.Now.AddDays(-7) - DateTime.Now.AddDays(8);
        double dayss = ts.TotalDays;
 
Share this answer
 
v2

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