lundi 13 juillet 2015

Inner List in mvc Model is not getting updated

Employee class

Public Class Employee
Public Property ID As Integer
Public Property Name As String
Public Phonenumbers As IList(Of PhoneNumber)
Public Sub New()
    Phonenumbers = New List(Of PhoneNumber)
    Phonenumbers.Add(New PhoneNumber)
    Phonenumbers.Add(New PhoneNumber)
End Sub
End Class

PhoneNumber Class

Public Class PhoneNumber
Public PhoneNumber As Integer
Public Sub New()
    Me.PhoneNumber = 123
End Sub
End Class


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


@Using (Html.BeginForm())

End Using

@Html.ActionLink("Back to List", "Index")

@Section Scripts 
End Section

I have used EditorTemplates(folder) where template for Employee and Phone are saved those are

Employee.cshtml(in editor Templates)

@Modeltype Employee
<div class="form-horizontal">
<hr />
@Html.ValidationSummary(True, "", New With {.class = "text-danger"})
@Html.HiddenFor(Function(x) x.ID)

<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.TextBoxFor(Function(model) model.Name, New With {.htmlAttributes = New With {.class = "form-control"}})
        @Html.ValidationMessageFor(Function(model) model.Name, "", New With {.class = "text-danger"})
<div class="form-group">
    @Html.Label("phones", htmlAttributes:=New With {.class = "control-label col-md-2"})
    <div id="PhoneNumbers" class="col-md-10">
        @Html.EditorFor(Function(x) x.Phonenumbers)
<div class="form-group">
    <div class="col-md-offset-2 col-md-10">
        <input type="submit" value="Create" class="btn btn-default" />

PhoneNumber.vbhtml(in Editors Template)

@modeltype PhoneNumber
ViewData("Title") = "Phone"
End Code

<div class="container">
<div class="row">
    @Html.Label("phone", htmlAttributes:=New With {.class = "control-label col-md-2"})
    <div class="col-md-10 PhoneNumber">
        @Html.TextBoxFor(Function(model) model.PhoneNumber)

now whenever I am posting data and receive post the class object employee always comes with old data. no data for phone list in class employee gets updated!!!

Employee Controller

Imports System.Web.Mvc

Namespace Controllers
Public Class EmpController
    Inherits Controller
    ' GET: Emp/Create
    Function Create() As ActionResult
        Dim employee = New Employee()
        Return View(employee)
    End Function
    ' POST: Emp/Create
    Function Create(ByVal emp As Employee) As ActionResult
            ' TODO: Add insert logic here
            Return RedirectToAction("Index")
            Return View()
        End Try
    End Function
 End class
End Namespace

above is my controller in which, at the post time I am not getting any updated data for emp, although I get name property correctly but for inner list(of phonenumbers) I get always value default(123) which was given.

Aucun commentaire:

Enregistrer un commentaire