Saturday, March 19, 2011

File ActionResult in ASP.net MVC

          You can return a file using File ActionResult Type in ASP.net  MVC. The url will download the file which specified in the Action. Lets check some samples.
    
       MIME Types:

       Word (.doc)

      application/msword
     Word (.docx)
      application/vnd.openxmlformats-officedocument.wordprocessingml.document
        PDF (.pdf)
      application/pdf
    more

For Pdf file format It will render it on browser. In the case of a word file (.doc,.docx) it will be downloaded


         public ActionResult Help()
        {
            return File("/Content/Help.pdf""application/pdf");
        }



In Chrome


IE 9 will render the pdf file in default pdf viewer.here is the result 




Result in firefox




When we want to download a doc file. use the following action


      public ActionResult Help()
        {
            return File("/Content/sample.doc""application/msword");
        }
The .doc file sample.doc will be downloaded.
In Chrome

In IE 9







Remote Validator in asp.net mvc 3


Remote Validator

ASP.NET MVC 3 supports the new RemoteAttribute class that enables you to take advantage of the jQuery Validation plug-in's remote validator support. This enables the client-side validation library to automatically call a custom method that you define on the server in order to perform validation logic that can only be done server-side.
In the following example, the Remote attribute specifies that client validation will call an action namedUserNameAvailable on the UsersController class in order to validate the UserName field.
public class User {
    [Remote("UserNameAvailable", "Users")]
    public string UserName { get; set; }
}
The following example shows the corresponding controller.
public class UsersController { 
    public bool UserNameAvailable(string username) 
    { 
        if(MyRepository.UserNameExists(username)) 
        { 
            return "false"; 
        } 
        return "true"; 
    } }
For more information about how to use the Remote attribute.
The above content is Taken from www.asp.net  help file read full from here