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

I want to create like this demo i do not know how i can do that in asp.net mvc entity framework

https://i.stack.imgur.com/XdNdN.gif[^]

What I have tried:

This is my view :

<pre>@using (Html.BeginForm())
{
        <div>
            <span>Start Date :</span> <input type="date" name="start" />
            <span>End Date :</span> <input type="date" name="end" />
            <input type="submit" value="Get Records Between Dates" />
        </div>
        <table>
        @foreach (var item in Model)
        {
            <tbody>
               <tr>
                    <td>
                         @Html.DisplayFor(modelItem => item.expenses.Expenses_Type)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.DateExpense)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.Amount)
                    </td>                           
            </tbody>
         }
        </table>

}


And this my controller :

public ActionResult Index(DateTime? start, DateTime? end)
   {
       var ExpenseDetails = _context.ExpenseDetails.Include(s => s.expenses).ToList();

       return View(ExpenseDetails);
   }
Posted
Updated 19-Apr-19 19:06pm
Comments
[no name] 19-Apr-19 15:37pm    
We don't know what date fields are in expenses. That's what you compare.
Member 14317227 19-Apr-19 15:46pm    
no it is any date , not specified date so he can enter and comber any date
Bryian Tan 19-Apr-19 15:43pm    
have you try to use the .Where condition?
Member 14317227 19-Apr-19 15:48pm    
yes i use this

public ActionResult Index(DateTime? start, DateTime? end)
{
var ExpenseDetails = _context.ExpenseDetails.Include(s => s.expenses).Where(t => t.DateExpense >= start && t.DateExpense <= end).ToList();

return View(ExpenseDetails);
}

but when i run application there are no any data
Member 14317227 19-Apr-19 15:49pm    
can you check if the code is correct please

1 solution

First, you need a controller that takes the dates as parameters, they are posted on your form. Then you need to query your data to get values between the dates.

The code you posted seems reasonable. If you want help, you need to say more than 'does not work'. Tell us what you tried. Are the date times being sent to the back end? If not, have you used Fiddler to see what is being posted?

I think you may need to set 'id' as well as 'name' for the values to be posted?
 
Share this answer
 
Comments
Member 14317227 20-Apr-19 2:39am    
Thank you the code is work , But how i can show all data before i filter or select the date , so the user can see all data and when he want the filter by date he can do that also ?
Christian Graus 20-Apr-19 3:01am    
I do it like this....


public ActionResult Index(DateTime? start, DateTime? end)
{
var ExpenseDetails = _context.ExpenseDetails.Include(s => s.expenses).Where(t => (!start.HasValue || t.DateExpense >= start) && (!end.HasValue || t.DateExpense <= end)).ToList();

return View(ExpenseDetails);
}

As you used DateTime?, your query works with no paramaters. And now your code will, too. So just call it initially and post back to it with values to filter

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