Friday, November 12, 2010

Solving Request.Files.Count is 0 in mvc

Solving problem while uploading files in ASP.NET MVC 2

You should set both name and id attributes for fileupload control.

You should set enctype attribute of form element into multipart/form-data

Try your self uploading file.

See the following Example for more details

<form method="post" enctype="multipart/form-data" action="MyController/Upload">
    <input type="file" class="file" id="File1" name="File1" />
    <input type="submit" id="Submit1" class="btn80" value="Upload" />
</form>



public ActionResult Upload()
{
    foreach (string upload in Request.Files)
    {
        var file = Request.Files[upload];
        string filePath = "C:\\Temp\\" + DateTime.Now.Ticks.ToString() + file.FileName;
        file.SaveAs(filePath);
                  
    }
    return View();
}

Thursday, October 28, 2010

Javascript callback with parameter

Javascript delegates with parameter:

Here is one example for calling a javascript function using a delegate ( function pointer ) .

While calling a method using delegate, you should use the first parameter as 'this' (or an object), after that you can pass additional parameters.

For more details see the following example.

Eg:

pass a function as parameter.

do_AjaxPostWithData(url,function (str) { });


the following function calls the delegate with a parameter.
function do_AjaxPostWithData(actionUrl, callback) {

callback.call(
 
this,context.responseText);

}

Wednesday, September 29, 2010

Defining Structural Contracts

Defining Structural Contracts
Data Contracts:

Data contracts are the contractual agreement about the format and structure of the payload data (that is, the SOAP body) in the messages exchanged between a service and its consumer.
Data contracts are the preferred WCF way to enable serialization of complex types included in operation signatures, either as parameters or as return values.

Using a data contract, you can specify exactly how the service expects the data to be formatted as XML. The data contract is used by a data contract serializer in WCF client applications to describe how to serialize the data for parameters into XML, and by a data contract serializer in the service to deserialize the XML back into data values that it can process. Values returned by a service are similarly serialized as XML and transmitted back to the client application, which deserializes them.


DataContract Attribute
The DataContract Attribute, defined in the System.Runtime.Serialization namespace, is used to declare a type as a Data contract.
It can be applied to enums, structs, and classes only.


DataMember Attribute
The DataMember Attribute, also part of the System.Runtime.Serialization namespace, is applied only to members and is used to declare that the member should be included in the serialization of the data structure.


EnumMember Attribute
The final Data contract–related attribute to consider is the EnumMemberAttribute. As the name suggests, it is used to declare that a given element of an enum that has been declared with a DataContractAttribute should be considered as part of the Data contract.

Eg: Create a WCF Service and write the following code in the IService.cs file.

[ServiceContract]
public interface IService
{
    [OperationContract]
    Employee GetEmployee();
}

[DataContract]
public class Employee
{
    string name, job;
    [DataMember]
    public string Name
    {
        set { name = value; }
        get { return name; }
    }
    [DataMember]
    public string Job
    {
        set { job = value; }
        get { return job; }
    }
}

Write the following code in the Service.cs

public class Service : IService
{
    public Employee GetEmployee()
    {
        Employee emp = new Employee
        {
            Name = "Jobin John",
            Job = "Software Eng"
        };
        return emp;
    }
}


Add a new website to Solution. Place a button and write the following code.

        protected void btnDisplay_Click(object sender, EventArgs e)
        {
            EmployeeServiceReference.ServiceClient client = new WebApplication1.EmployeeServiceReference.ServiceClient();

            EmployeeServiceReference.Employee emp = client.GetEmployee();
            Response.Write("Name : " + emp.Name);
            Response.Write("
Job : " + emp.Job); }

Returning Views in MVC Application

Create an ASP.NET MVC 2 empty application. Add a controller named HomeController and write the following action method.

public ViewResult Index()
        {
            return View();
        }


 

ActionResult Class:

All methods within a controller are Action methods, and returns ActionResult object.

The above function returns ViewResult
object; which is a subclass of ActionResult

Adding View:

    Right click in the method and select add view option. Name the View as "Index" and add the view.

Run the application. Hence you have created an MVC application which returns a View.

jQuery Selectors: Select elements using Id

Selecting HTML elements with Id.

syntax:
$("#elementid")

The # selector selects an element with a unique id.

eg:
write the following in html head section
<script type="text/javascript">
$(
}
function DisplayAll() {"#divDetails").text("Welcome to my first jQuery application.");</script>

write the following in the body.

<div id="divDetails"></div><hr />
<div></div><hr /><div></div><hr /><div></div><hr />

Tuesday, September 14, 2010

Using jQuery Selectors : Using tag name

Using jQuery Selectors
  tag name, ID, and class. They can be used either on their own or in combination with other selectors.             
Three building blocks of selectors are

Selecting HTML elements with tag name.


The following example demonstrates how to select all div elements and assign a text value into it within  an HTML page.
syntax:
 $(element name)  : which returns all matched html elements.
 $('element name').text('text to display'); : which assign the text value into all matched element.
Eg:
Add following in html head

<script type="text/javascript">
$(
}
function DisplayAll() {"div").text("Welcome to my first jQuery application.");</script>
Add following in html body

<input id="Button1" type="button" value="Display Text onclick="DisplayAll()" /><div></div><hr />
<div></div><hr /><div></div><hr /><div></div><hr />