NickP
NickP

Reputation: 1414

Using a dropdown list for model

I am trying to use the following dropdown box to select a range of values to edit for the model.

So far I have gotten the following code to work:

@Html.DropDownList("Services", "")

But essentially I want to save that string here instead of this:

@Html.EditorFor(Function(model) model.ServiceName)

My view is:

@Using Html.BeginForm()
    @Html.ValidationSummary(True)
    @<fieldset>
        <legend>RequestedService</legend>

        <div class="editor-label">
            @Html.LabelFor(Function(model) model.ServiceId)
        </div>
        <div class="editor-field">
            @Html.EditorFor(Function(model) model.ServiceId)
            @Html.DropDownList("Services", "")
            @Html.ValidationMessageFor(Function(model) model.ServiceId)
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
End Using

With both things currently.

My controller:

Function AddService(id As Integer) As ViewResult
        Dim serv As RequestedService = New RequestedService
        serv.JobId = id

        Dim ServiceList = New List(Of String)()

        Dim ServiceQuery = From s In db.Services
                           Select s.ServiceName

        ServiceList.AddRange(ServiceQuery)

        ViewBag.Services = New SelectList(ServiceList)

        Return View(serv)
    End Function

And finally my model:

Imports System.Data.Entity
Imports System.ComponentModel.DataAnnotations

Public Class RequestedService

Public Property RequestedServiceId() As Integer


Public Property ServiceId() As Integer

<Required()>
<Display(Name:="Job Number *")>
Public Property JobId() As Integer

<Required()>
<Display(Name:="Station ID *")>
Public Property StationId() As Integer

End Class

Upvotes: 1

Views: 150

Answers (1)

Jorge
Jorge

Reputation: 18237

The problem it's with the SelectList you need to tell to the seleclist, which are the value and what's the display text. You cannot passed only the List of string, to populated correctly, add the key and the value like this

Dim ServiceQuery = (From s In db.Services
                       Select s)

Could be like this In Case that you need the ID related to the service

ViewBag.Services = New SelectList(ServiceList, s.IDServices, s.ServiceName)

Or like this In Case that you need the only the text for the value

ViewBag.Services = New SelectList(ServiceList, s.ServiceName, s.ServiceName)

UPDATE

To Accomplish this you need to modify the View and Your action.

First in your action change the name of your Viewbag element like this

ViewBag.ServiceId = New SelectList(ServiceList, s.IDServices, s.ServiceName)

Now the obvious change in your view would be

@Using Html.BeginForm()
@Html.ValidationSummary(True)
@<fieldset>
    <legend>RequestedService</legend>

    <div class="editor-label">
        @Html.LabelFor(Function(model) model.ServiceId)
    </div>
    <div class="editor-field">
        @Html.DropDownList("ServiceId", "")
        @Html.ValidationMessageFor(Function(model) model.ServiceId)
    </div>

    <p>
        <input type="submit" value="Create" />
    </p>
</fieldset>

End Using

So you don't need the

@Html.EditorFor(Function(model) model.ServiceId)

When the user, select the option from the dropdownlist and click the create button, the attribute ServiceID will be automatically mapped into your class, that's mvc3 works with the name of the element's to do the all the magic work for you

Upvotes: 1

Related Questions