Click here to Skip to main content
14,453,349 members
Rate this:
Please Sign up or sign in to vote.
I have a controller that is creating some columns for my WebGrid based on the names of some fields. For example, I have a field that is titled "e_qty", and the "e_" means that it should end up being an editable field. So the function is there that creates the textbox columns for the WebGrid if needed (it works fine). I'm passing the datatable as a list of objects to the View. Normally this will populate my WebGrid with all the values just fine. It's when I have textboxes in my WebGrid that it doesn't add the actual values. I can set the "value" property of the textboxes in the controller, but that defeats the purpose of sending the list of objects to the view. So the question is: Can you use a model to populate a WebGrid that has textboxes without having to set the "value" property? I'm just wanting to know if what I'm trying to do is possible.


Here is how I'm bringing the model over from the controller and filling the WebGrid:

@ModelType System.Collections.Generic.IEnumerable(Of Object)
@Code
    Layout = "~/Views/Shared/_VisualRuleLayout.vbhtml"
End Code

<link href="~/Content/bootstrap.css" rel="stylesheet" />

@Using Html.BeginForm("Cancel", "GridView", FormMethod.Post)
    @<text>
        <div Class="form-horizontal">
            <form>
                <h3>@ViewBag.Title</h3>

                @Code
                    Dim grid = New WebGrid(Model, canPage:=True, canSort:=True, rowsPerPage:=50)
                    grid.Pager(WebGridPagerModes.NextPrevious)
            End Code

        <div>
                @Code
                    @grid.GetHtml(htmlAttributes:=New With {
                        .id = "grid",
                        .class = "table table-bordered table-striped table-condensed",
                        .name = "grid1"},
                        emptyRowCellValue:="No Records Found",
                        headerStyle:="grid-header",
                        columns:=ViewBag.Columns)
                End Code
            </div>
        </div>


What I have tried:

I've tried setting the WebGrid's cells back to just plain cells and that works, but I need the editable columns to show as textboxes with the cell's value already populated in it.

I've also tried setting the e_ columns to have hidden textboxes hoping that once the edit button is clicked, the textbox can be unhidden and the value will be set to it's "value" property. But this didn't work because the original value from the model still did not fill the WebGrid cell because of the hidden textbox.
Posted
Updated 1-Aug-19 4:39am
v2

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100