ASP.NET C#

ASP.NET MVC5 Form 관련

ToKor 2020. 11. 28. 02:49

- Form Tag 생성

// 퓨어 html 출력시
<form action=”/Home/CreatePerson/MyIdValue”, class=”personClass” data-formType=”person” method=”post”>

// Helper Method

#Basic
@using(Html.BeginForm()){ … }

#Parameters
@using(Html.BeginForm(action, controller, routeValues, method, attributes))

#실제 사용예시
@using(Html.BeginForm(“CreatePereson”, “Home”, new{id=”MyIdValue” }, FormMethod.Post,
     new {@class = “personClass”, data_formType=”person”})) { … }

- HIDDEN

// Form Tag
<input type=”hidden” id=”documentId” name=”documentId” value=”@Model.documentId”>

// Helper Tag
@Html.HiddenFor(model => model.documentId)

- SELECT

// Form Tag
<label for=”cities”>Select a City</label>
<select name=”cities” id=”cities”>
  <option value=”toronto”>Toronto</option>
  <option value=”london”>London</option>
  <option value=”Milton”>Milton</option>
</select>

// DDL Helper
@Html.DropDownListFor(model => model.Cities, new SelectList(new [] {
  “Toronto”, “London”, ”Milton”}), “Select”, 
  new{Class=”form-control”}
)

// DDL Helper - enum
public enum ClassType
{
  Trial = 1,
  Paid = 2
}
@Html.EnumDropDownListFor(model => model.ClassType, “Select”, new{@class=”form-control”})

- RADIO

// Form Tag
<label for=”toronto">Toronto</label>
<input type=”radio” id=”toronto" name=”cities” value=”toronto”>
<label for=”london">London</label>
<input type=”radio” id=”london" name=”cities” value=”london”>
<label for=”milton">Milton</label>
<input type=”radio” id=”milton" name=”cities” value=”milton”>

// Helper Method
public class Student
{
  public int StudentId {get; set;}
  public string StudentName {get; set;}
  public string Cities{get; set;}
}

@model Student

@Html.RadioButtonFor(model => model.Cities, “toronto")Toronto
@Html.RadioButtonFor(model => model.Cities, “london")London
@Html.RadioButtonFor(model => model.Cities, “milton")Milton

- CHECKBOX

// Form Tag
// Value가 있으면, 서브밋될때 밸류값이 전달된다.
<input type=”checkbox” id=”toronto" name=”toronto" value=”inGTA” checked>
<label for=”toronto”>Toronto</label>
<input type=”checkbox” id=”london" name=”london" value=”outGTA”>
<label for=”london”>London</label>

// Helper Method
@model Student

@Html.CheckBoxFor(model => model.Cities, new {Style = “font-size:10px}”)

- TEXT

/ Form Tag
<input type="text" id="cities" name="cities" />

/ Helper Method
@Html.TextBoxFor(model => model.cities, new {@class =”form-control”})
@Html.EditorFor(model => model.cities, new{htmlAttributes = new{@class=”form-control”}})
@Html.EditorFor(model => model.cities, new{htmlAttributes= new{Class=”form-control”, Type=”data”, Required=”required”, Readonly=”readonly”}})
@class 를 Class로 사용

- TEXTAREA

// Form Tag
<textarea id="cities" name="cities" rows="3" cols="10">

// Helper Method
@Html.TextAreaFor(model => model.cities, new{rows=”3”, cols=”10”})