idipous
idipous

Reputation: 2910

MVC 4 C# Calendar Widget from Action-View

I have created a web calendar using HTML,CSS and C# in an MVC application. The HTML is in a view and I have a controller with an action that initializes the Calendar and displays the correct days on the view.

It works as intended but the problem comes from the fact that I want to embed this calendar to a couple of pages in unrelated Views controlled by different actions and with different models loaded.

I could copy paste the view code and make the necessary adjustments to the model but that would not be ideal. I tried with RenderAction but the problem is that when I use the buttons to navigate to different months I leave the initial view. e.g.:

@Html.ActionLink("‹", "Calendar", 
new { month = Model.RequestedDateTime.Month - 1, year = 
Model.RequestedDateTime.Year })

Is there a way to modify this behavior? Thanks

Upvotes: 0

Views: 1838

Answers (2)

idipous
idipous

Reputation: 2910

I solved the problem in a similar way, to what developer10214 proposed. I used a div and in that I used Render.Action. Then I used the following code

 $(function () {
 $("#cal").on('click', "#backwards", function () {
 $.ajax({
    url: "Home/Calendar?target=backwards",
    cache:false,
    type: "GET",
    success: function (result) {
        $("#cal").html(result);
    }
});
});
});

in the including page. And I did not use ActionLink at all.

Upvotes: 0

developer10214
developer10214

Reputation: 1186

Html.ActionLink, alway creates a normal link, with have the common behaviour. If you want to share your calender. To avoid redundant code, you may redesign the calender as a partial view and replace the link by a label for example. Bind an ajax function to the label-click-event to refresh the calender. Something like:

@Html.Label("<", new { onclick="pageMonthBack("+Model.RequestedDateTime.Month - 1+", " Model.RequestedDateTime.Year+");" })

I would place the called ajax functions in a script which is loaded separately.

Upvotes: 1

Related Questions