How do i insert data with dropdownlist in ASP.NET MVC VB

时间:2017-05-16 09:39:27

标签: asp.net asp.net-mvc vb.net asp.net-mvc-4

i have the following problem: When i create a new db entry, i type the values in the boxes. But i want to exchange one box for a dropdownmenu (Wich you can see in the View), but i have absolutely no Idea how i can do that. Is this possible?

Greetings :)

View:

    @Code
    ViewData("Title") = "Create"
End Code

<h2>Create</h2>

@Using (Html.BeginForm()) 
    @Html.AntiForgeryToken()

    @<div class="form-horizontal">
        <h4>Dezernate</h4>
        <hr />Ausgewähltes Dezernat:

        @Html.ValidationSummary(true)
    <div class="form-group">
        @Html.LabelFor(Function(model) model.Dezernat, New With { .class = "control-label col-md-2" })
        <div class="col-md-10">
            @*<select id="dezernat" name="Dezernate">
             <option value="dez-1">dez-1</option>
             <option value="dez-2">dez-2</option>
             <option value="dez-3">dez-3</option>
             <option value="dez-f">dez-f</option>
             <option value="dez-fm">dez-fm</option>
             <option value="dez-agus">dez-agus</option>
         </select>*@
            @Html.EditorFor(Function(model) model.Dezernat)
            @Html.ValidationMessageFor(Function(model) dezernat)
        </div>
    </div>

Controller

 Public Class NeuController
        Inherits System.Web.Mvc.Controller

        Private db As New TempDbContext

        ' GET: /Neu/
        Function Index() As ActionResult
            MsgBox("Daten wurden erfolgreich gespeichert!", vbOKOnly, "Erfolgreich!")
            Return RedirectToAction("Create", "Neu")
            Return View("Create")
        End Function

        ' GET: /Neu/Create
        Function Create() As ActionResult
            Return View()
        End Function

        ' POST: /Neu/Create
        'Aktivieren Sie zum Schutz vor übermäßigem Senden von Angriffen die spezifischen Eigenschaften, mit denen eine Bindung erfolgen soll. Weitere Informationen 
        'finden Sie unter http://go.microsoft.com/fwlink/?LinkId=317598.
        <HttpPost()>
        <ValidateAntiForgeryToken()>
        Function Create(<Bind(Include := "ID,Dezernat,Rechner,Scan,Photoshop")> ByVal dezernate As Dezernate) As ActionResult
            If ModelState.IsValid Then
                db.Dezernate1.Add(dezernate)
                db.SaveChanges()
                Return RedirectToAction("Index")
            End If 
            Return View(dezernate)
        End Function

1 个答案:

答案 0 :(得分:0)

以下是解决方案:

变化:

<div class="form-group">
    @Html.LabelFor(Function(model) model.Dezernat, New With { .class = "control-label col-md-2" })
    <div class="col-md-10">
        <select id="dezernat" name="Dezernate">
         <option value="dez-1">dez-1</option>
         <option value="dez-2">dez-2</option>
         <option value="dez-3">dez-3</option>
         <option value="dez-f">dez-f</option>
         <option value="dez-fm">dez-fm</option>
         <option value="dez-agus">dez-agus</option>
     </select>
        @Html.EditorFor(Function(model) model.Dezernate)
        @*@Html.ValidationMessageFor(Function(model) model.Dezernat)*@
    </div>
</div>

到:

<div class="form-group">
    @Html.LabelFor(Function(model) model.Dezernat, New With { .class = "control-label col-md-2" })
    <div class="col-md-10">
        <select id="dezernat" name="Dezernat">
         <option value="dez-1">dez-1</option>
         <option value="dez-2">dez-2</option>
         <option value="dez-3">dez-3</option>
         <option value="dez-f">dez-f</option>
         <option value="dez-fm">dez-fm</option>
         <option value="dez-agus">dez-agus</option>
     </select>
        @Html.ValidationMessageFor(Function(model) model.Dezernat)
    </div>
</div>

非常感谢User3250!