Accepting A IENUMERABLE model post in a controller in mvc 3 vb.net?
I have a view in my app that uses a @modeltype ienumerable of (xxxxxxx.abc), and uses a for each loop to populate the view. This view has select boxes that show up next to each item in the view. When I post the form to the controller accepting it as a formcollection does not work, and if I accept it like: ByVal abc as abc
it says that its an invalid model.. The code is as follows:
<AcceptVerbs(HttpVerbs.Post)>
Function ClassAttendance(ByVal Attendance As Attendance) As ActionResult
If ModelState.IsValid Then
UpdateModel(Attendance)
db.SaveChanges()
Return RedirectToAction("Index")
End If
Return View()
End Function
Any ideas?? Can I somehow use a for each loop in the view without making it ienumerable? If so when It posts to the controller my below code would just about work.. The only value that really matters is the selectlist choice and the id of the record that it was changed for...
The view is:
@ModelTYPE List(Of xxxxx.attendance)
@Code
ViewData("Title") = "Class Attendance Record"
End Code
@Using Html.BeginForm
@<fieldset>
<table>
<tr>
<th>
Firs开发者_C百科t Name
</th>
<th>
Last Name
</th>
<th>
Registrant ID
</th>
<th>
Course Status
</th>
<th>
Comments
</th>
</tr>
@For r As Integer = 0 To ViewBag.count
Dim i As Integer = r
@Html.HiddenFor(Function(m) m(i).id)
@<tr>
<td>
@Html.DisplayFor(Function(m) m(i).firstName)
</td>
<td>
@Html.DisplayFor(Function(m) m(i).lastName)
</td>
<td>
@Html.DisplayFor(Function(m) m(i).reg_id)
</td>
<td>
@Html.DisplayFor(Function(m) m(i).Completed_Class)
</td>
<td>
@Html.DropDownList("Completed_Class", New SelectList(ViewBag.courseStatus, "Status", "Status"))
</td>
<td>
@Html.TextBoxFor(Function(m) m(i).Comments, New With {.class = "AttenComment"})
</td>
</tr>
Next
</table>
<p>
<input type="submit" name="submit" />
</p>
</fieldset>
End Using
I have been over countless tut's and posts with no luck at all... I only want to update the model record for each record to the corresponding selected value from the selectlist... Thanks greatly for any and all help..
In brief, this can be done. There are many ways to accomplish this, the easiest by far I found was to have a custom view model which contains an array of the model you want to bulk insert.
public class MultipleAttendance
{
public Attendance[] attendances {get;set;}
}
Then in your View you can loop as such:
@for(int i=0,i<10;i++)
{
@Html.TextBox(attendances[i].firstName)
}
In the controller
[HttpPost]
public ActionResult Create(MultipleAttendance model)
{
// Get a value
model.attendances[0].firstName;
}
Hope it helps :)
精彩评论