How to pass date parameter from view to controller in MVC

Analyst_SQL 3,576 Reputation points
2023-03-09T02:19:27.43+00:00

I want to pass date parameter from View to controller and retrieve data on the base of date parameter.

C_Date is my date parameter

below is my table class

  public class CustomerMV
    {

        public int CustomerID { get; set; }
        public string CustomerName { get; set; }
        public string Name { get; set; }
        public string ContentType { get; set; }
        public byte[] Data { get; set; }
        public string AName { get; set; }
        public string I_Status { get; set; }
        public Nullable<int> EmpID { get; set; }
        public string C_Address { get; set; }

        [DataType(DataType.Date, ErrorMessage = "Date only")]
        [DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]


        public Nullable<System.DateTime> C_Date { get; set; }
    }
}

Controller

public class CustomerController : Controller
    {
        SilverProductionEntities DB = new SilverProductionEntities();

        // GET: Customer
        public ActionResult CustomerList()
        {
            var list = new List<CustomerMV>();
            var customers = DB.Customers.ToList().Where(t => t.Data != null );

            //var tblAccountControls = DB.tblAccountControls.Include(t => t.tblBranch).Include(t => t.tblCompany).Include(t => t.tblUser);
            foreach (var customer in customers)
            {
                list.Add(new CustomerMV {  CustomerID = customer.CustomerID, CustomerName = customer.CustomerName,Data=customer.Data
                 
                });

            }
            return View(list);
        }
    }
}

@model IEnumerable<ERP_APP.Models.CustomerMV>

@{

}



<div class="card">
    <div class="card-header">
        <div class="text-sm">
            <div class="card-title">Item List</div>
        </div>
    </div>
    <div class="card-body">
        @Html.ActionLink("Create Customer", "CreaterCustomer", null, new { @class = "btn btn-primary" })
        <hr />

        <div class="form-horizontal">


            <div class="form-group form-group-sm">
                <div class="col-md-8">
                   
                    @Html.TextBox("start", null, new { htmlAttributes = new { @class = "form-control", @id = "datetimepicker1" } })
                </div>
            </div>
            <div class="form-group form-group-sm">
                <div class="col-md-8">
                    @Html.TextBox("end", null, new { @class = "form-control datepicker" })
                </div>
            </div>
            <div class="form-group-sm">
                <div class="col-md-offset-2 col-md-10">
                    <input type="submit" value="Save" class="btn btn-default" /> <span> </span>
                </div>
            </div>
        </div>
        <table class="table table-striped my-4 w-100" id="datatable2">
            <thead>
                <tr>
                    <th>

                        @Html.DisplayNameFor(model => model.CustomerID)
                    </th>
                    <th>

                        @Html.DisplayNameFor(model => model.CustomerName)
                    </th>
                    <th>

                        @Html.DisplayNameFor(model => model.Data)
                    </th>





                    <th>Action</th>
                </tr>
            </thead>
            <tbody>

                @foreach (var item in Model)
                {
                    <tr>
                        <td>
                            @Html.DisplayFor(modelItem => item.CustomerID)
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.CustomerName)
                        </td>

                        <td>

                            <img src="data:image/jpeg;base64,@(Convert.ToBase64String(item.Data))" width="50" height="30">
                        </td>

                        <td>
                            @Html.ActionLink("Edit", "EditItem", new { customerid = item.CustomerID }, new { @class = "btn btn-warning" }) |




                            @Html.ActionLink("Delete", "Delete", new { }, new { @class = "btn btn-danger" })
                        </td>
                    </tr>
                }

            </tbody>
        </table>
    </div>
</div>

SQL table,I want date filter on C_Date

Create table #customer(CustomerID int,CustomerName varchar(50),C_Date date)

insert into #customer values(1,'Akhter','2023-03-01')

insert into #customer values(1,'Akhter','2023-03-05')

insert into #customer values(1,'Akhter','2023-03-05')

insert into #customer values(1,'Akhter','2023-03-06')

insert into #customer values(1,'Akhter','2023-03-07')

Developer technologies ASP.NET Other
Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. Lan Huang-MSFT 30,186 Reputation points Microsoft External Staff
    2023-03-09T07:05:14.0333333+00:00

    Hi @Analyst_SQL ,

    You can use FormMethod.post to pass the date parameters from view to controller.

    According to the format of Input Date, you need to change the DataFormatstring to "{0:MM-dd-yyyy}".

    You can refer to the following code:

     @using (Html.BeginForm("Index", "Home", FormMethod.Post))
                {
                    <div class="form-horizontal">
                        <div class="form-group form-group-sm">
                            <div class="col-md-8">
    
                                <input name="start" type="date"  id="datepicker" class="form-control" />
                            </div>
                        </div>
                        <div class="form-group form-group-sm">
                            <div class="col-md-8">
                                <input name="end" type="date" id="datepicker" class="form-control" />
                            </div>
                        </div>
                        <div class="form-group-sm">
                            <div class="col-md-offset-2 col-md-10">
                                <input type="submit" value="Save" class="btn btn-default" /> <span> </span>
                            </div>
                        </div>
                    </div>
                }
    
    [HttpPost]
            public ActionResult Index(DateTime? start, DateTime? end)
            {
               
                var Test = (from q in DB.customer
                           where q.C_Date > start && q.C_Date < end
                           select q).ToList();
    
                return View(Test);         
            }
    

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    2 people found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.