ASP.net MVC + Dropdown + VB.net

개요



Transaction의 입력 보조로서 Master의 내용을 DropdownList에 취득하고 Transaction의 갱신 처리를 행한다.

이미지





환경



Windows7 32bit SP1
Microsoft SQL Server 11.00.3000
Visual Studio2013 Professional Version12.0 Update4
VB.net

구현



모델

Employee.vb
Public Class Employee
 Public Property Id As Integer
 Public Property Name As String
 Public Property CountryId As String
End Class

Country.vb
Public Class Employee
 Public Property Id As Integer
 Public Property CountryId As String
 Public Property CountryName As String
End Class

SampleContext.vb
Public Class SampleContext
    Inherits DbContext
 Public Sub New()
  MyBase.New("SampleContext")
 End Sub
 Public Property Employees As DbSet(Of Employee)
 Public Property Countries As DbSet(Of Country)
End Class

제어

EmployeeControl.vb
Public Class EmployeeController
    Inherits Controller
 <HttpGet>
 Function Index() As ActionResult
  Return View(db.Employees.ToList())
 End Function

 <HttpGet>
 Function Create() As ActionResult
  Dim emp As New Employee
  Dim ct = db.Countrys.ToList()
  ViewBag.model1 = New SelectList(ct, "Id", "CountryId", emp.CountryId)
  ViewBag.model1 = db.Countries.[Select](Function(m) New SelectListItem() With {
                            .Text = m.CountryName, 
                            .Value = m.CountryId})
  Return View()
 End Function

 <HttpPost>
 Function Create(<Bind(Include:="ID,Name,CountryId")> ByVal employ As Employee) As ActionResult
  If ModelState.IsValid Then
   db.Employees.Add(employ)
   db.SaveChanges()
   Return RedirectToAction("Index")
  End If
  Return View(employ)
 End Function
End Class

보기

Index.vbhtml
@modeltype  DropdownHoge.Models.Employee
@Code
    ViewData("Title") = "Create"
End Code
@Using (Html.BeginForm())
@Html.AntiForgeryToken()
    @<div class="form-horizontal">
         <h4>Employee</h4>
         <hr />
        @Html.ValidationSummary(True, "", New With {.class = "text-danger"})
         <div class="form-group">
             @Html.LabelFor(Function(model) model.Name, htmlAttributes:=New With {.class = "control-label col-md-2"})
             <div class="col-md-10">
                 @Html.EditorFor(Function(model) model.Name, New With {.htmlAttributes = New With {.class = "form-control"}})
                 @Html.ValidationMessageFor(Function(model) model.Name, "", New With {.class = "text-danger"})
             </div>
         </div>
         <div class="form-group">
             @Html.LabelFor(Function(model) model.CountryId, htmlAttributes:=New With {.class = "control-label col-sm-2"})
             <div class="col-sm-10">
                 @Html.DropDownListFor(Function(model) model.CountryId, DirectCast(ViewBag.model1, IEnumerable(Of SelectListItem)),"SELECT", New With{.class="form-control"})
             </div>
         </div>
         <div class="form-group">
             <div class="col-md-offset-2 col-md-10">
                 <input type="submit" value="Create" class="btn btn-default" />
             </div>
         </div>
    </div>

End Using

Create.vbhtml
@modeltype  DropdownHoge.Models.Employee
@Code
    ViewData("Title") = "Create"
End Code

<h2>Create</h2>

@Using (Html.BeginForm())
    @Html.AntiForgeryToken()
    @<div class="form-horizontal">
         <h4>Employee</h4>
         <hr />
        @Html.ValidationSummary(True, "", New With {.class = "text-danger"})
         <div class="form-group">
             @Html.LabelFor(Function(model) model.Name, htmlAttributes:=New With {.class = "control-label col-md-2"})
             <div class="col-md-10">
                 @Html.EditorFor(Function(model) model.Name, New With {.htmlAttributes = New With {.class = "form-control"}})
                 @Html.ValidationMessageFor(Function(model) model.Name, "", New With {.class = "text-danger"})
             </div>
         </div>
         <div class="form-group">
             @Html.LabelFor(Function(model) model.CountryId, htmlAttributes:=New With {.class = "control-label col-sm-2"})
             <div class="col-sm-10">
                 @Html.DropDownListFor(Function(model) model.CountryId, DirectCast(ViewBag.model1, IEnumerable(Of SelectListItem)),"SELECT", New With{.class="form-control"})
             </div>
         </div>
         <div class="form-group">
             <div class="col-md-offset-2 col-md-10">
                 <input type="submit" value="Create" class="btn btn-default" />
             </div>
         </div>
    </div>

End Using

화면





마지막으로



여기까지 힘든 것이 힘들었다. 이해가 부족한 곳이 많기 때문에, 이해 후, 부분 뷰를 사용해도 시험해 본다.

좋은 웹페이지 즐겨찾기