Showing posts with label MVC 2. Show all posts
Showing posts with label MVC 2. Show all posts

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();
}

Wednesday, September 29, 2010

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.

Thursday, September 9, 2010

First MVC 2 Application with Visual Studio 2010

Creating your First MVC Application with Visual Studio 2010

Eg: Follow the following steps.

  • Create an empty MVC 2 web application.


  • Add a controller named HomeController and write the following method.
public string Index()
{
  return "My first MVC Application";
}

  • Run the application.

The above application just returns a string value. We dont have any view. The browser will display only a string.

ASP.NET MVC Folder Conventions

ASP.NET MVC Folder Conventions


App_Data : Contains database files. For example, the App_Data folder might contain a local instance of a SQL Server Express database.

Content : Contains static content such as images and Cascading Style Sheet files.

Controllers : Contains ASP.NET MVC controller classes.

Models : Contains ASP.NET MVC model classes.

Scripts : Contains JavaScript files including the ASP.NET AJAX Library and jQuery.

Views : Contains ASP.NET MVC views.

Introduction to MVC 2 with Visual Studio 2010

Microsoft MVC 2 in Visual Studio 2010


The Microsoft ASP.NET MVC framework is Microsoft’s newest framework for building web applications. The ASP.NET MVC framework was created to support pattern-based software development. In other words, the framework was designed to make it easier to implement software design principles and patterns when building web applications.

An MVC application, a Model View Controller application, is divided into the following three parts:

• Model: An MVC model contains all of an application’s logic. The model includes all of an application’s validation logic, business logic, and data access logic.

• View: An MVC view contains HTML markup and view logic.

• Controller: An MVC controller contains control-flow logic. An MVC controller interacts with MVC models and views to control the flow of application execution. which handle incoming requests, perform operations on the domain model, and choose a view to render back to the user