2013-03-03 57 views
0

這是一個mvc3,Razor視圖引擎,vb.net項目。我被困在一個地方,它或多或少是一個問題,從來沒有做過這樣的事情,直到現在..屬性表單提交失敗值MVC3

基本上這個問題的肉是我有一個視圖,可以有1到多個複選框它取決於表中列出的項目數量。複選框將全部使用該表中提供的名稱。用戶檢查過的任何複選框將在模型傳回時保存在控制器中。

除提交時,所有內容都正常工作..我是失去了模型的機構屬性一起被點擊提交按鈕時..

@ModelType xxxxxx.CourseModel 

@Code 
ViewData("Title") = "Edit Courses" 
End Code 

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script> 
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script> 


@Using Html.BeginForm(Nothing, Nothing, FormMethod.Post, New With {.enctype = "multipart/form-data"}) 
@Html.ValidationSummary(True) 

    @<fieldset> 
    <legend>Edit Courses</legend> 
    @Html.HiddenFor(Function(model) model.cId) 
    <table style="float: left"> 
    <tr> 

    <th>Certification Bodies</th> 
    </tr> 
    <tr> 
    @For _i As Integer = 0 To Model.Bodies.Count - 1 
      Dim i = _i 
      @<td>@Html.CheckBoxFor(Function(model) model.Bodies.ElementAt(i).certSelected)@Html.DisplayFor(Function(f) f.Bodies.ElementAt(i).certName)@Html.HiddenFor(Function(model) model.Bodies.ElementAt(i).certBodyId)</td> 
     Next 

    </tr> 
    <tr><th><input type="submit" value="Save" /></th></tr> 
    </table> 

和型號目前如下:

Public Class CourseModel 
     Private _cId As Integer 
     Public Property cId() As Integer 
     Get 
     Return _cId 
     End Get 
     Set(ByVal value As Integer) 
     _cId = value 
     End Set 
     End Property 
     Private m_Bodies As New List(Of CertBodyVM) 
    Public Property Bodies() As List(Of CertBodyVM) 
    Get 
     Return m_Bodies 
    End Get 
    Set(value As List(Of CertBodyVM)) 
     m_Bodies = value 
    End Set 
    End Property 
    End Class 

這是CertBodyVM

Public Class CertBodyVM 
Private _certName As String 
Public Property certName() As String 
    Get 
     Return _certName 
    End Get 
    Set(ByVal value As String) 
     _certName = value 
    End Set 
End Property 
Private _certSelected As Boolean 
Public Property certSelected() As Boolean 
    Get 
     Return _certSelected 
    End Get 
    Set(ByVal value As Boolean) 
     _certSelected = value 
    End Set 
End Property 
Private m_certBodyId As Integer 
Public Property certBodyId() As Integer 
    Get 
     Return m_certBodyId 
    End Get 
    Set(ByVal value As Integer) 
     m_certBodyId = value 
    End Set 
End Property 
End Class 

最後加載這個視圖控制器功能..

Function EditCourse(ByVal id As Integer) As ViewResult 

     Dim courses As New CourseModel 
     Dim newCourse As cours = db.courses.Single(Function(c) c.course_id = id) 

     courses.cId = newCourse.course_id 

     courses.cRef = newCourse.course_ref 

     Dim cBodies As List(Of certbody) = db.certbodies.Where(Function(F) F.confNum = _AnnualNumber AndAlso F.Display = 1).ToList 
     For Each _cb In cBodies 
      Dim cb = _cb 
      Dim x As New CertBodyVM 
      x.certBodyId = cb.idCertBodies 
      x.certName = cb.CertBodyName 
      Try 
       Dim csetBodies As coursecertifybody = db.coursecertifybodies.Where(Function(f) f.cert_Body_id = cb.idCertBodies AndAlso f.course_ref = newCourse.course_ref AndAlso f.course_id = newCourse.course_id).FirstOrDefault 
       If Not IsNothing(csetBodies) Then 
        x.certSelected = True 
       Else 
        x.certSelected = False 
       End If 
      Catch ex As Exception 
       x.certSelected = False 
      End Try 
      courses.Bodies.Add(x) 
     Next 

     Return View(courses) 
    End Function 

在SO標準,我包括下面的柱控制器功能,因爲我可以放在_eCourse VAR一看,看的興趣那些屍體是空的,沒有什麼比以下更需要了。

<AcceptVerbs(HttpVerbs.Post)> 
    Function EditCourse(ByVal _eCourse As CourseModel) As ActionResult 
    'Do something with _eCourse 
    Return RedirectToAction("Blah") 
    End Function 

任何想法,當模型被提交時,我失去了Bodies屬性?

回答

1

集合的模型綁定不適用於.elementAt(i)。相反,嘗試使用這樣的:在vb.net中使用

@Html.CheckBoxFor(Function(model) model.Bodies[i].certSelected) 
@Html.DisplayFor(Function(f) f.Bodies[i].certName) 
@Html.HiddenFor(Function(model) model.Bodies[i].certBodyId) 
+0

索引「(」「)」,而不是「[」「]」,但比該解決方案是點上的其他..謝謝..得多* – Skindeep2366 2013-03-04 02:15:13

+1

faceplam *當然是VB。接得好。 – 2013-03-04 02:26:21