1

I am trying to essentially render a "listing" of records with the ability to edit each line record. I am doing something wrong in assigning my Function() in the @HtmlHelper extensions. I am trying to use the out-of-the-box scaffolding as a guide. Here's my simplified HTML code:

@ModelType IEnumerable(Of Models.Affiliate.ProspectEvent)

<div>
 <table class="table">
   <tr>
     <th> @Html.DisplayNameFor(Function(model) model.EventDate) </th>
     <th> @Html.DisplayNameFor(Function(model) model.Details) </th>
   </tr>
   @For Each item In Model
     @Using (Html.BeginForm())
       @Html.AntiForgeryToken
         @<tr>
           <td> @Html.TextBoxFor(Function(modelItem) item.EventDate) </td>
           <td>
               @Html.TextAreaFor(Function(modelItem) item.Details, 5, 50, Nothing)
           </td>
           <td> <input type="submit" class="btn btn-sm btn-default" value="Save" /> </td>
         </tr>
     End Using
   Next
 </table>
</div>

When the formdata gets back to the controller, it is prefixed with "$VB$Local_item". I can filter out the prefix in the controller with Prefix:="$VB$Local_item",... but I know that my problem is in the "Function(modelItem)" in the HTML and that's where it needs to be fixed, but I can't figure out what to use. I know that I should know this but I don't. Any direction would surely be appreciated.

HumbleBeginnings
  • 1,009
  • 10
  • 22
  • You cannot use a `foreach` loop to generate form controls for a collection (refer [this answer](http://stackoverflow.com/questions/30094047/html-table-to-ado-net-datatable/30094943#30094943)). But your view makes no sense - you generating multiple forms and you can only submit one form at at time. –  May 16 '16 at 23:07
  • No - I just tried to simplfy a single iteration. Sorry about that. I'll check your link now. Thank you! – HumbleBeginnings May 16 '16 at 23:12
  • @StephenMuecke I reviewed your link and, to be honest, I am still confused. My code above actually works (as long as I strip the prefix). If I change Model(i) to a local variable I come up with the same scenario. I am creating multiple forms in the HTML and only submitting one (a single "record" of the "Prospect Event" that will have been edited and "Saved". Is there a better way to do this? -- as I've been doing things this way for some time. – HumbleBeginnings May 17 '16 at 00:54
  • You UI really makes no sense. If you have a collection and you want to edit it then use a `for` loop or `EditorTemplate` for typeof `ProspectEvent` inside one form element (and post back the whole collection and save all changes at once. If you only want to edit one item, generate links for each items that redirects to an Edit view that edits one item. –  May 17 '16 at 01:19
  • Its adds the prefix because you are creating an form control for `item.EventDate`, (which is exactly what it should do - and if it didn't everything else about model binding would fail) but you model does not contain a property name `item` which contains a property named `EventDate` so therefore you need to do a hack to fix it. –  May 17 '16 at 01:21
  • 1
    I understand your approach about submitting all the items together. As I mentioned in my OP, I was using the "List" of the CRUD scaffolding as a guide. In that, they use a for/each and the "Function(modelItem) item.???" is used there but of course it is not being submitted back. I will continue to work with it with your comments in mind -- and I DO thank you for your reply!! – HumbleBeginnings May 17 '16 at 01:27

0 Answers0