0

Preciso de criar uma função para introduzir um novo defeito, que está associado a um ou vários processos(por enquanto ainda não descobri como posso seleccionar varios), mas quero que os processos apareçam numa dropboxlist, ja fiz isto na função de editar e funcionou, o problema é que na função criar n consigo. Vou deixar o código que criei para a função editar e para a função criar e as views referentes, agradeço desde ja a quem me possa ajudar.

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

    ' POST: DEF_DEFECT/Create
    'To protect from overposting attacks, please enable the specific properties you want to bind to, for 
    'more details see http://go.microsoft.com/fwlink/?LinkId=317598.
    <HttpPost()>
   <ValidateAntiForgeryToken()>
    Function Create(<Bind(Include:="CODE_DEF,DESCRIPTION_DEF")> ByVal dEF_DEFECT As DEF_DEFECT) As ActionResult
        If ModelState.IsValid Then
            Dim processToUpdate = db.DEF_DEFECT _
                           .Include(Function(i) i.PRO_PROCESS) _
                           .Where(Function(i) i.CODE_DEF = dEF_DEFECT.CODE_DEF) _
                           .Single()

            Dim processSelected As String = Request.Form("ProcessesList").ToString()

            UpdateDefectProcess(processToUpdate, processSelected)

            db.Entry(processToUpdate).State = EntityState.Modified

            db.SaveChanges()
            Return RedirectToAction("Index")
        End If
        Return View(dEF_DEFECT)
    End Function'

A view create

            Code
            <div>
                @Html.EditorFor(Function(model) model.CODE_DEF, New With {.htmlAttributes = New With {.class = "form-control"}})
                @Html.ValidationMessageFor(Function(model) model.CODE_DEF, "", New With {.class = "text-danger"})
            </div>
        </div>

        <div class="form-group">

            Description
            <div>
                @Html.EditorFor(Function(model) model.DESCRIPTION_DEF, New With {.htmlAttributes = New With {.class = "form-control"}})
                @Html.ValidationMessageFor(Function(model) model.DESCRIPTION_DEF, "", New With {.class = "text-danger"})
            </div>
        </div>
        <div class="form-group">
            Process
            <div>

'Dá sempre o erro aqui' @Html.DropDownList("ProcessesList", CType(ViewBag.PList, IEnumerable(Of SelectListItem)), New MultiSelectList(Model.PRO_PROCESS, Model.selectedProcesses, "CODE_PRO")) End Using

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

A view Edit

Code
    <div>
        @Html.EditorFor(Function(model) model.CODE_DEF, New With {.htmlAttributes = New With {.class = "form-control"}})
        @Html.ValidationMessageFor(Function(model) model.CODE_DEF, "", New With {.class = "text-danger"})
    </div>
</div>
<div class="form-group">
    Description
    <div>
        @Html.EditorFor(Function(model) model.DESCRIPTION_DEF, New With {.htmlAttributes = New With {.class = "form-control"}})
        @Html.ValidationMessageFor(Function(model) model.DESCRIPTION_DEF, "", New With {.class = "text-danger"})
    </div>
</div>
<div>
    <div>
        <label for="ProcessCode">Process</label>
        @Html.DropDownList("ProcessesList", CType(ViewBag.PList, IEnumerable(Of SelectListItem)), New MultiSelectList(Model.PRO_PROCESS, Model.selectedProcesses, "CODE_PRO"))
    </div> 
</div>
<div class="form-group">
    <div>
        <input type="submit" value="Save" class="btn btn-default" />

Edit

 ' GET: DEF_DEFECT/Edit/5
    Function Edit(ByVal id As String) As ActionResult
        If IsNothing(id) Then
            Return New HttpStatusCodeResult(HttpStatusCode.BadRequest)
        End If

        Dim dEF_DEFECT As DEF_DEFECT = db.DEF_DEFECT.Find(id)
        If IsNothing(dEF_DEFECT) Then
            Return HttpNotFound()
        End If

        PopulateProcessesDropDownList(dEF_DEFECT.PRO_PROCESS)

        Return View(dEF_DEFECT)
    End Function


    Sub PopulateProcessesDropDownList(selectedprocesses As Object)
        ' Dropdown Lists
        Dim allProcesses = From s In db.PRO_PROCESS
              Order By s.CODE_PRO
              Select s.CODE_PRO, s.DESCRIPTION_PRO Distinct

        ViewBag.PList = New SelectList(allProcesses, "CODE_PRO", "CODE_PRO", selectedValue:=selectedprocesses)
    End Sub

    ' POST: DEF_DEFECT/Edit/5
    'To protect from overposting attacks, please enable the specific properties you want to bind to, for 
    'more details see http://go.microsoft.com/fwlink/?LinkId=317598.
    <HttpPost()>
    <ValidateAntiForgeryToken()>
    Function Edit(<Bind(Include:="CODE_DEF,DESCRIPTION_DEF")> ByVal dEF_DEFECT As DEF_DEFECT) As ActionResult
        If ModelState.IsValid Then
            Dim processToUpdate = db.DEF_DEFECT _
                           .Include(Function(i) i.PRO_PROCESS) _
                           .Where(Function(i) i.CODE_DEF = dEF_DEFECT.CODE_DEF) _
                           .Single()

            Dim processSelected As String = Request.Form("ProcessesList").ToString()

            UpdateDefectProcess(processToUpdate, processSelected)

            db.Entry(processToUpdate).State = EntityState.Modified

            db.SaveChanges()
            Return RedirectToAction("Index")
        End If
        Return View(dEF_DEFECT)
    End Function
    Private Sub UpdateDefectProcess(updateDefect As DEF_DEFECT, processSelected As String)
        'Dim selectedProcessHS = New HashSet(Of String)(processSelected)
        Dim selectedProcessHS = processSelected

        Dim defectProcess As IEnumerable(Of String) = New HashSet(Of String)(updateDefect.PRO_PROCESS.Select(Function(c) c.CODE_PRO))
        For Each c In db.PRO_PROCESS
            If selectedProcessHS.Contains(c.CODE_PRO.ToString()) Then
                updateDefect.PRO_PROCESS.Add(c)
            Else
                If defectProcess.Contains(c.CODE_PRO) Then
                    updateDefect.PRO_PROCESS.Remove(c)
                End If
            End If

        Next

    End Sub
4
  • Dê uma inspecionada no model que está sendo passado para ver se não está nulo. Veja no controller se está passando o model para a view. Commented 24/10/2017 às 15:29
  • Marco sou muito nova nisto e ja apanhei a aplicação a meio... Pode ser mais explicito.?
    – Somebody
    Commented 24/10/2017 às 15:31
  • veja se no action do controller está passando algo como return view(model) e se o model está nulo. trabalho com c#. não sei como ficaria no vb Commented 24/10/2017 às 15:33
  • Estou mesmo perdida... Em cima eu postei o controller...
    – Somebody
    Commented 24/10/2017 às 15:40

1 Resposta 1

0

Consegui..

Adicionei no modelo DEF_Defect

 Public Overridable Property selectedProcesses As ICollection(Of PRO_PROCESS) = New HashSet(Of PRO_PROCESS)

e na view create do def_defect substitui o codigo por:

<div class="form-group">
             Process Code                 
                 @Html.DropDownList("ProcessesList", CType(ViewBag.ProcessCode, IEnumerable(Of SelectListItem)))
                 @Html.ValidationMessageFor(Function(model) model.selectedProcesses, "", New With {.class = "text-danger"})
         </div>

E no controller ficou assim:

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

    ' POST: DEF_DEFECT/Create
    'To protect from overposting attacks, please enable the specific properties you want to bind to, for 
    'more details see http://go.microsoft.com/fwlink/?LinkId=317598.
    <HttpPost()>
    <ValidateAntiForgeryToken()>
    Function Create(<Bind(Include:="CODE_DEF,DESCRIPTION_DEF")> ByVal dEF_DEFECT As DEF_DEFECT) As ActionResult
        If ModelState.IsValid Then
            db.DEF_DEFECT.Add(dEF_DEFECT)

            Dim processSelected As String = Request.Form("ProcessesList").ToString()

            UpdateDefectProcess(dEF_DEFECT, processSelected)

            db.SaveChanges()
            Return RedirectToAction("Index")

        End If
        Return View(dEF_DEFECT)
    End Function

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .