Next step of programming

Just another WordPress.com weblog

Posts Tagged ‘MVC

Event Calendar in MVC application Using jMonthCalendar(JQuery)

with 8 comments

A good Jquery plugin is available as Event Calendar which can be download from following location.

http://www.bytecyclist.com/projects/jmonthcalendar/
or
http://code.google.com/p/jmonthcalendar/

I used it in one sample project with MVC and loaded certain events from the controller. Which can in return load from the Model. But for the time being in this sample i returned from the controller it self.


public ActionResult Events(string date)
{
MyEvents event1 = new MyEvents            
{
EventID = 11 ,
StartDate = new DateTime(2009,04,01),
Title = "Meeting with Boss",
Description = "Meeting on product release",
CssClass = "Meeting",
URL= "/Home/EventDetails/11"            
};

MyEvents event2 = new MyEvents            
{
EventID = 12,
StartDate = new DateTime(2009, 04, 01),
Title = "Celebration",
Description = "Fathers birthday",
CssClass = "Birthday",
URL = "/Home/EventDetails/12"            
};

string[] stringlist = new string[2];
stringlist[0] = event1.GetJSONResult();
stringlist[1] = event2.GetJSONResult();

return Json(stringlist);
}


For calling the events it did getJson call from the index page where the jMonthCalendar is shown. It will call one of the function the Controller and in return will get the array of the events which will then be sent to the jMonthCalendar. It will load the events. We can add the URL also so that when user clicks the event he will get the complete detail once again with the help of the controller


<script type="text/javascript">
    $().ready(function() {
var options = {

height: 650,
width: 980,
navHeight: 25,
labelHeight: 25,

onMonthChanging: function(dateIn) {
$.getJSON("http://localhost:1511/Home/Events",
function(data) {

//Array of my events                    
var events = new Array();

//Loop and load all the events and load them into the array
                    $.each(data, function(i, item) {

var oResultData = eval('(' + item + ')');
var event = { "EventID": 5, "Date": oResultData.StartDate,
"Title": oResultData.Title, "URL": oResultData.URL,
"Description": oResultData.Description,
"CssClass": oResultData.CssClass
};

events.push(event);

});

//Load the events into the calendar
                    $.jMonthCalendar.ReplaceEventCollection(events);
$.jMonthCalendar.DrawCalendar(dateIn);
});
return true;
},
onEventBlockOver: function(event) {
return true;
},
onEventBlockOut: function(event) {
return true;
},
onDayLinkClick: function(date) {
return true;
},
onDayCellClick: function(date) {
return true;
}
};

var newevents = [];
$.jMonthCalendar.Initialize(options, newevents);
});</script>


Download the sample from following location

EventCalendar

Here are the screen shot of the application.

Event Calendar in MVC Application

Event Calendar in MVC Application

Detail of selected Event

Detail of selected Event

Written by A.Sethi

April 30, 2009 at 5:25 pm

Posted in ASP.NET

Tagged with , , , ,

File upload in ASP.NET MVC

with 7 comments

Here is a sample for handling upload of a file, In your MVC application. In this sample i used the Dialog box of JQuery, where user will select the file and will click upload


<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">    

<h2>Files uploaded to server</h2>    

<div id="dialog" title="Upload files">        
    <% using (Html.BeginForm("Upload", "File", FormMethod.Post, new { enctype = "multipart/form-data" }))
    {%><br />
        <p><input type="file" id="fileUpload" name="fileUpload" size="23"/> ;</p><br />
        <p><input type="submit" value="Upload file" /></p>        
    <% } %>    
</div>
<a href="#" onclick="jQuery('#dialog').dialog('open'); return false">Upload File</a>
</asp:content>


Then we have to handle this request in the respective controller which is specified in BeginForm of our above code (FileContoller) and action name (Upload). Following will be our code in the FileController Controller for the Upload action


public void Upload()
{
foreach (string inputTagName in Request.Files)
{
HttpPostedFileBase file = Request.Files[inputTagName];
if (file.ContentLength > 0)
{
string filePath = Path.Combine(HttpContext.Server.MapPath("../Uploads")
, Path.GetFileName(file.FileName));
file.SaveAs(filePath);
}
}

RedirectToAction("Index", "File");
}


Download the sample code from here

File upload in MVC with JQuery Dialog

File upload in MVC with JQuery Dialog

Written by A.Sethi

April 17, 2009 at 10:55 am

Posted in ASP.NET

Tagged with , , ,

Using JQuery UI in ASP.NET MVC

with 17 comments

I was developing one application in ASP.NET MVC, where i was in need of displaying Date Picker to the user. But if you generate View with the help of MVC extension then it will display text box for your date field. In order to show the datepicker i used jquery datepicker.
You can download it from JQuery UI here

Step 1

  • Include these files in your project. in the script folder
  • jquery-1.3.2.js
    ui.core.js
    ui.datepicker.js

    Also include the theme folder in project to apply the themes for the respective controls

  • Add the Script reference in your webpage, for that include the following code
  • <script type="text/javascript" src="/../../Scripts/jquery-1.3.2.js"></script>
    <script type="text/javascript" src="/../../Scripts/ui.core.js"></script>
    <script type="text/javascript" src="/../../Scripts/ui.datepicker.js"></script>
    
  • Include the following script in your view page(where you want to show the date picker) giving it a ID. On that ID we will use it as a input. ID should be unique
  • <script type="text/javascript">
     $(document).ready(function(){
    $("#FromDate").datepicker();
    });
    </script>
    

Step 2

  • We need to create a Extension for HtmlHelper Class.
  • using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    namespace System.Web.Mvc.Html
    {
    public static class DatePickerExtension
    {
    public static string DatePicker(this HtmlHelper htmlHelper, string name)
    {
    return "<input type=\"text\" id=\"" + name + 
    "\" name=\"" + name + "\" value=\"\"/>";
    }
    }
    }
  • In your view page you have to add the following code to have that control
  •  <label for="fromDate">From date:</label>             
    <%= Html.DatePicker("FromDate")%>

Output :

JQuery UI DatePicker

JQuery UI DatePicker

Written by A.Sethi

April 10, 2009 at 8:45 am

Posted in ASP.NET

Tagged with , , , , ,