Displaying data in View based on which button is clicked: ASP.NET MVC3 RAZOR?

First of all I would never separate the 2 methods for month and year as they are exactly the same if you change the method signature to include a from and to date for example.

Up vote 0 down vote favorite 1 share g+ share fb share tw.

I have a Program Scheduling view. When the view loads using GET, it shows a list of programs (in a table) available in the database. It lists only 5 programs (max) randomly.

There are two buttons provided 1) Show March Programs. When this button is clicked the table should show only programs that is scheduled for March month. 2) Show 2012 Programs.

When this button is clicked the table should show only programs that is scheduled for year 2012. How do we achieve it? Note: Also, can you please suggest a solution that will work if the button for "Show 2012 Programs" is replaced with a dropdown list for years.

Based on MVC principle: The UI logic belongs in the view. Input logic belongs in the controller. Business logic belongs in the model.

1) How do we achieve it in quick way? 2) How do we achieve it standard way (following MVC principle listed above) GET Image CODE namespace MyEventOriginIdentificationTest. Controllers { public class ProgramSchedule { public int ProgramID { get; set; } public string ProgramName { get; set; } public int ScheduledDate { get; set; } public int ScheduledMonth { get; set; } public int ScheduledYear { get; set; } } public class ProgramTetecastController : Controller { List wholeProgramList = new List() { new ProgramSchedule { ProgramID = 1,ProgramName = "2012-March-15", ScheduledDate = 15,ScheduledMonth=3,ScheduledYear=2012 }, new ProgramSchedule { ProgramID = 2,ProgramName = "2012-March-16", ScheduledDate = 16,ScheduledMonth=3,ScheduledYear=2012 }, new ProgramSchedule { ProgramID = 3,ProgramName = "2012-April-11", ScheduledDate = 11,ScheduledMonth=4,ScheduledYear=2012 }, new ProgramSchedule { ProgramID = 4,ProgramName = "2013-Jan-05", ScheduledDate = 5,ScheduledMonth=1,ScheduledYear=2013 } }; public List GetProgramsScheduleForMonth(int theMonth) { var monthProgram = from prog in wholeProgramList where prog.

ScheduledMonth == theMonth select prog; return ((List)monthProgram); } public List GetProgramsScheduleForYear(int theYear) { var yearProgram = from prog in wholeProgramList where prog. ScheduledYear == theYear select prog; return ((List)yearProgram); } // GET: public ActionResult MyProgramSchedule() { return View(wholeProgramList); } // POST: HttpPost public ActionResult MyProgramSchedule(FormCollection collection) { try { return RedirectToAction("ProgramSchedule"); } catch { return View(); } } } } VIEW @model IEnumerable @{ ViewBag. Title = "MyProgramSchedule"; } MyProgramSchedule ProgramID ProgramName ScheduledDate ScheduledMonth ScheduledYear @foreach (var item in Model) { @Html.

DisplayFor(modelItem => item. ProgramID) @Html. DisplayFor(modelItem => item.

ProgramName) @Html. DisplayFor(modelItem => item. ScheduledDate) @Html.

DisplayFor(modelItem => item. ScheduledMonth) @Html. DisplayFor(modelItem => item.

ScheduledYear) } READING Store Attributes from Button in a Hidden field ASP. NET MVC3 RAZOR: Retrieving Hidden field Attribute value in Controller (using ViewModel) Issue with multiple views on single view Hide or Disable MVC3 ActionLinks depending on cell value ASP. NET MVC ActionFilterAttribute inject value before model binding asp.net asp.net-mvc asp.net-mvc-3 mvc razor link|improve this question edited Feb 22 at 13:20 asked Feb 21 at 6:44Lijo68318 73% accept rate.

First of all I would never separate the 2 methods for month and year as they are exactly the same if you change the method signature to include a from and to date for example. With this in mind you can do something like: public ActionResult ShowProgramsBetweenDates(DateTime? From, DateTime?

To) { if(from. HasValue && to. HasValue) { return View(GetProgramsBetweenDates(from.

Value, to. Value)); } return View(wholeProgramList); } public IEnumerable GetProgramsBetweenDates(DateTime from, DateTime to) { return from prog in wholeProgramList where prog. ScheduledDate >= from && prog.

ScheduledDate } and a simple JQuery to help $('form inputtype="button"'). On('click', function() { // for all buttons in the form $('#from'). Val($(this).

Attr('data-from')); // set #from with date-from $('#to'). Val($(this). Attr('data-to')); // set #to with date-to $(this).

Closest('form').submit(); // submit the form }); As a principle, and to help your throughout the application lifetime you should never have more than one value for a date, there is no need to split up dates in a "table" as you can extract everything you need from a Date field for example, your class could simply be as public class ProgramSchedule { public ProgramSchedule() {} // Always add an empty constructor public int ProgramID { get; set; } public string ProgramName { get; set; } public int ScheduledDate { get; set; } } so your DataObject would be something like: List wholeProgramList = new List() { new ProgramSchedule { ProgramID = 1, ProgramName = "2012-March-15", ScheduledDate = new DateTime(2012, 3, 15) }, new ProgramSchedule { ProgramID = 2, ProgramName = "2012-March-16", ScheduledDate = new DateTime(2012, 3, 16) }, new ProgramSchedule { ProgramID = 3, ProgramName = "2012-March-11", ScheduledDate = new DateTime(2012, 3, 11) }, new ProgramSchedule { ProgramID = 4, ProgramName = "2012-January-5", ScheduledDate = new DateTime(2012, 1, 5) }, }; and in your View, you simple do: @foreach (var item in Model) { @Html. DisplayFor(modelItem => item. ProgramID) @Html.

DisplayFor(modelItem => item. ProgramName) @Html. DisplayFor(modelItem => item.

ScheduledDate) @item.ScheduledDate. ToString("MMM") @item.ScheduledDate. ToString("yyyy") }.

Thanks for the response. I got this warning "Attribute 'data-from' is not a valid attribute of element 'input'". Also, when I run I got a javascript error saying " Object doesn't support this property or method" inside the js function for click.

How do we correct it? – Lijo Feb 21 at 8:52 1 the warning is because you have the document set to anything that is not HTML5 (worry not, data- attributes even work in IE6). Regarding the javascript, have you loaded the latest jQuery (1.7.1) at all?

You need to include either in that view or in your layout page... – balexandre Feb 21 at 8:59 1 you need 1.7.1 as I said, 1.5.1 has to much bugs and on method does not exist, use Nuget to update it or simple add – balexandre Feb 21 at 9:22 1 it's not $('this') it's $(this) ... my mistake, I wrote it automatically and never look back, sorry about that. So the last line is $(this). Closest('form').submit(); – balexandre Feb 21 at 12:03 BTW, remember to accept the answer if the answer have helped you in any way... 71% acceptance rate is not that good.

– balexandre Feb 21 at 12:05.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions