Help us understand the problem. What is going on with this article?

ASP.net MVC + Dropdown + VB.net

More than 3 years have passed since last update.

概要

Transactionの入力補助として、Masterの内容を、DropdownListに取得し、Transactionの更新処理を行う。

イメージ

image.png

環境

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

実装

Model

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

Control

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

View

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

画面

kekka.png

最後に

ここまでいきつくのが大変だった。理解が足りないところが多いので、理解後、部分ビューを使っても試してみる。

khashimoto
自分のことで・・・・精一杯の・・・
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした