pass data between mvc controllers 4

1

Hello is my first question on the page. I have 2 controllers but one of them has a partial view and contains a razor variable that I would like to get its value in the main view.

 Function Index() As ActionResult          
       Return View()
 End Function


Function Modal_Encuesta(codigo As String) As ActionResult
    ViewBag.idcodigo = codigo          
    Return PartialView()
End Function

How do you see a variable pass from the index view to the partial view but how do I do it the other way around? pass a razor variable to the Index controller and thus pass it to the view.

    
asked by Heiner Said 15.11.2016 в 16:46
source

1 answer

0

What you show is the driver but the model is missing and the view is missing.

The model that in this case we only have one field we would define it as:

Public Class ModeloIndex
  Public Property codigo As String
    ' aqui puedes añadir mas campos
End Class

The View that includes a form with a field

<div>
    @Using Html.BeginForm("Modal_Encuesta", "Home", New With {.ReturnUrl =      ViewBag.ReturnUrl}, FormMethod.Post, New With {.class = "form-horizontal", .role  = "form"})
        @Html.TextBoxFor(Function(m) m.codigo, New With {.class = "form-control"})
        @<text>
            <input type="submit" value="Submitir" Class="btn btn-default" />
       </text>
    End Using
</div>

And the controller:

' Para llegar aquí con GET la url sería: http://... ~/Home/Index?codigo=Valor

 Function Index(m As ModeloIndex) As ActionResult
    'el valor de codigo viene y va en el modelo en m.codigo
    Return View(m)
 End Function
'
Function Modal_Encuesta(m As ModeloIndex) As ActionResult
    '
    '//ViewBag.idcodigo = m.codigo
    '//el ViewBag no sirve pues el valor ya llega a la vista en m
    '  
    '
    '//Tiene que existir una vista llamada Index.cshtml o Modal_Encuesta.cshtml 
    '//Falta decidir el resto de la estructura. Tal como está funciona con única Vista 
    Return PartialView("Index",m)
End Function

I think the question is very basic and the answer is too. It is a functional skeleton to start assembling forms with MVC and Visual Basic

I hope this helps you.

    
answered by 15.12.2016 / 00:13
source