Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,

Does anyone have any code (preferably VB.NET) that refreshes the "Index.vbhtml" HTML table (auto-generated when you add a new Controller in Visual Studio 2010) showing the list of amended records in a table after a CRUD event?

For example, if I have a list of "persons" in my Index view:

(1) I click on the auto-generated RAZOR "Create New" button I have now written it so that a Jquery dialog pops up with a partial view of the "Create.vbhtml" form, this allows me to create a new person record.
(2) then, after I click the "Submit" button the new record is passed back to the Create (POST) action method as a serialized string and is duly saved to the database and the Jquery dialog closes...

Q: how can I get the HTML table on Index.vbhtml to show the new record - but - without doing a complete page refresh? I understand that this could be done using JSON and Jquery .post(), but my attempts at doing it that way has not gone well!

I believe I need return some JSON from the Action Method (I just seem to get a 500 Internal Error when I do that) and to stick something in the "Success" function of my .post() method, but I cannot work out what this should be.

Many thanks in advance!
Posted
Updated 14-Mar-12 2:57am
v2

1 solution

I found this great article that walked me through the process of creating a MVC application that updates a HTML table without having to redraw the whole web-page that contains it. The link to Brian Lachniet's blog post is provided below:

http://blachniet.com/2011/08/10/partial-views-with-unobtrusive-ajax/#more-23

It is written in C# but with a little effort I was able to convert it into VB.NET

Below is the VB.NET code that I created. Please refer back to Brian's blog to understand what the code is doing.

Controller:
VB
Namespace BoringStore
    Public Class ProductController
        Inherits System.Web.Mvc.Controller

        '
        ' GET: /BoringStore

        Public Function Index() As ActionResult
            Dim db As New BoringStoreContext
            Dim viewModel As New ProductIndexViewModel() With {.NewProduct = New Product(), .Products = db.Products}

            Return View(viewModel)
        End Function

        Public Function Index_AddItem(viewModel As ProductIndexViewModel) As ActionResult
            Dim db As New BoringStoreContext()
            db.Products.Add(viewModel.NewProduct)
            db.SaveChanges()

            Return PartialView("ProductListControl", db.Products)
        End Function

    End Class
End Namespace

Main view:
HTML
@* Views\Index.vbhtml *@

@ModelType BoringStore.ProductIndexViewModel

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

@Code
    ViewData("Title") = "Index"
End Code

<h2>Index</h2>

@Using Ajax.BeginForm("Index_AddItem", New AjaxOptions With {.UpdateTargetId = "productList"})
    @<fieldset>
        <legend>Resource</legend>

        <div class="editor-label">
            @Html.LabelFor(Function(model) model.NewProduct.Name)
        </div>
        <div class="editor-field">
            @Html.EditorFor(Function(model) model.NewProduct.Name)
        </div>
        <div class="editor-label">
            @Html.LabelFor(Function(model) model.NewProduct.Price)
        </div>
        <div class="editor-field">
            @Html.EditorFor(Function(model) model.NewProduct.Price)
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
End Using

<div id="productList">
    @Html.Partial("ProductListControl", Model.Products)
</div>

Partial view:
HTML
@* Views\ProductListControl.vbhtml (partial) *@

@ModelType IEnumerable(Of BoringStore.Product)

<table>

    @* Render the page headers *@

    <tr>
        <th>
            Name
        </th>
        <th>
            Price
        </th>
        <th></th>
    </tr>

    @* Render the name and price of each product. *@

    @For Each item In Model
        Dim currentItem = item
        @<tr>
            <td>
                @Html.DisplayFor(Function(modelItem) currentItem.Name)
            </td>
            <td>
                @Html.DisplayFor(Function(modelItem) currentItem.Price)
            </td>
        </tr>
    Next

</table>

ViewModel:
VB
' ViewModels\ProductIndexViewModel.vb

Public Class ProductIndexViewModel
    Public Property NewProduct() As Product
    Public Property Products() As IEnumerable(Of Product)
End Class
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900