반응형

방법 2. 와일드카드 맵핑

IIS 웹사이트 설정으로 간단하게 해결. IIS 7과 같이 깔끔한 주소로 동작한다.

홈디렉토리 >> 구성 >> 매핑 - 추가

실행파일 : C:\WINDOWS\Microsoft.Net\Framework\v2.0.5072\aspnet_isapi.dll

확장명    : .* (IIS 5 에서는 이렇게 해주고 실행파일쪽 마우스 클릭하면 확인이 활성화 된다...)

동사 - 모든동사, 다음으로 제한 선택 (다음으로제한 : GET, HEAD, POST, DEBUG)

스크립트엔진 선택

파일이 있는지 확인은 선택하지 않는다. (선택시 MVC 어플케이션 동작 안됨)


반응형
반응형
 ASP.NET MVC File Upload

This very short blog post will show you how to add support for uploading files to an ASP.NET MVC application.
Add a controller action

Add a controller action that accepts a parameter of type HttpPostedFileBase. In the example below, I save the file to my App_Data folder. I would highly recommend that you redirect the browser to another action when done.

public ActionResult Upload(HttpPostedFileBase file)
{
    var fileName = Path.Combine(Request.MapPath("~/App_Data"), Path.GetFileName(file.FileName));
    file.SaveAs(fileName);
    return RedirectToAction("Index");
}

Add an upload form

Add an upload form. Set the action to your upload controller action, the method to post, and very important, the enctype to multipart/form-data.

<form action="/Home/Upload" method="post" enctype="multipart/form-data">
    <label>Filename: <input type="file" name="file" /></label>
    <input type="submit" value="Submit" />
</form>
반응형
반응형

Let’s check how to work with dropdownlists on ASP.NET MVC web applications.

Basically the object you need to bind an object to a dropdownlist is the SelectList. The following overloads are available:

  • public SelectList(IEnumerable items);
  • public SelectList(IEnumerable items, object selectedValue);
  • public SelectList(IEnumerable items, string dataValueField, string dataTextField);
  • public SelectList(IEnumerable items, string dataValueField, string dataTextField, object selectedValue);

How to…

  • Create a new ASP.NET MVC Web application;
  • Right-click on Controller folder and select Add > Controller;
  • Rename it to CityController and click Add. The controller class is created;
  • Let’s write that is going to be our data source. This method builds a SelectList instance based on a generic list of cities;

public class City
{
    public string Name { get; set; }
    public int ID { get; set; }
}

public static SelectList GetCities(int index)
{
    List<City> cities = new List<City>();

    cities.Add(new City() { ID = 1, Name = “Sao Paulo” });
    cities.Add(new City() { ID = 2, Name = “Toronto” });
    cities.Add(new City() { ID = 3, Name = “New York” });
    cities.Add(new City() { ID = 4, Name = “Tokio” });
    cities.Add(new City() { ID = 5, Name = “Paris” });
    cities.Add(new City() { ID = 6, Name = “Lisbon” });

    SelectList selectList = new SelectList(cities, “ID”, “Name”, index);
    return selectList;
}

  • From the Index action method, let’s add the select list object to the ViewData dictionary and request the view to be rendered;

public ActionResult Index()
{
    ViewData["cities"] = GetCities(1);
    return View();
}

  • Right-click on the method you’ve just coded and select Add View;
  • Create a form using the Html helper method BeginForm and add a dropdownlist and a submit button. Your form will be similar to the following:

<% using (Html.BeginForm()) { %>
<%= Html.DropDownList(“lstCity”, ViewData["Cities"] as SelectList) %>
<input type=”submit” value=”Post” />
<% } %>

  • Build and run the application;
  • Navigating to the url http://localhost/City the view will be loaded with a dropdownlist and a submit button;
  • Let’s create now an action method that will handle the post when the user clicks the post button:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(FormCollection forms)
{
    int selectedItem = Convert.ToInt32(forms["lstCity"]);
    ViewData["cities"] = GetCities(selectedItem);
    return View();
}

Selecting a city on the dropdownlist and clicking on post button will raise a request that reaches the Index action method overload on CityController class that accepts a HttpVerb equals Post. In our simple example, the integer variable stores the id of the selected value on the form.

That is the simplest way to load and get the selected value of the dropdownlist.

반응형

+ Recent posts