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

Create.vbhtml.

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

<h2>Create</h2>

@Using (Html.BeginForm())

@Html.EditorForModel()
End Using

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

@Section Scripts 
@Scripts.Render("~/bundles/jqueryval")
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">
<h4>Employee</h4>
<hr />
@Html.AntiForgeryToken()
@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>
</div>
<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>
</div>
<div class="form-group">
    <div class="col-md-offset-2 col-md-10">
        <input type="submit" value="Create" class="btn btn-default" />
    </div>
</div>
</div>

PhoneNumber.vbhtml(in Editors Template)

@modeltype PhoneNumber
@Code
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)
    </div>
</div>
</div>

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
    <HttpPost()>
    Function Create(ByVal emp As Employee) As ActionResult
        Try
            ' TODO: Add insert logic here
            Return RedirectToAction("Index")
        Catch
            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