Mvc 3 texbox in webgrid (razor)
Asked Answered
S

7

6

Simple Q:How do you I get the textbox to show the value. Code below fail on item.LastName

@model List<Mvc2010_11_12.Models.Employee>
@{
    var grid = new WebGrid(source: Model,defaultSort: "FirstName",rowsPerPage: 3);
}

<div id="grid1">
    @grid.GetHtml(columns: grid.Columns(
        grid.Column("LastName"),
        grid.Column(format: (item) => Html.TextBox("LastName", item.LastName))
    ))
</div>
Subjoinder answered 16/11, 2010 at 14:54 Comment(0)
A
6

Extension methods (i.e., Html.TextBox) don't work well with dynamic objects (i.e., item)... it's a limitation of c#.

You've got a few options:

format: InputExtensions.TextBox(Html, "Last Name", item.LastName) // static call

format: Html.TextBox("Last Name", (object)item.LastName) // cast as non-dynamic object

format: &lt;input type="text" name="LastName" value="@item.LastName" /&gt; // avoid extensions

Also, I believe there's an inherent lambda with an "item" parameter - you shouldn't need to declare this yourself.

Antigorite answered 18/11, 2010 at 21:21 Comment(1)
I have taking the question to the blog of the presenter of the code I am modifying (Shiju Varghese). I think we need so guidance on best practice.Subjoinder
L
3

Quite convoluted but works:

@helper TextField(Employee employee, HtmlHelper<IEnumerable<Employee>> html)
{
    @html.TextBoxFor(x => employee.LastName)
}

<div id="grid1">
    @grid.GetHtml(columns: grid.Columns(
        grid.Column("LastName"),
        grid.Column(format: item => TextField(item.Value, Html))
    ))
</div>

Maybe there's a better approach though. Still learning the Razor syntax and quite frankly I am a bit disappointed by the WebGrid helper after having used MVCContrib Grid.

Leonteen answered 16/11, 2010 at 15:58 Comment(5)
There most be a easier way a doing it.Subjoinder
@user408698, I hope there is but I am a bit skeptical.Leonteen
Using this approach, when that form with textboxes is submitted back to a HttpPost action, how do you know which value belongs to which record?Euhemerize
@Boris B, you could include an additional hidden input containing the record id.Leonteen
I guess that means something like hidden id="RowId{N}" <=> textbox id="LastName{N}", N=1..RowCount. Thank you.Euhemerize
R
3

That one works for me

@model List<Mvc2010_11_12.Models.Employee>
@{
    var grid = new WebGrid(source: Model,defaultSort: "FirstName",rowsPerPage: 3);
}

<div id="grid1">
    @grid.GetHtml(columns: grid.Columns(
        grid.Column("LastName"),
        grid.Column(format: @<span>@Html.TextBox("LastName",@item.LastName as object)</span>   )
    ))
</div>
Rockett answered 30/12, 2010 at 16:52 Comment(1)
How to save the values from the textbox column in webgrid to a table in the database.Gefen
M
1

Try this:

grid.Column(format: (item) => Html.TextBox("LastName", (object) item.LastName))
Mckenna answered 18/5, 2011 at 23:25 Comment(0)
L
0

I had to cast object to string as follows:

format: (item) => Html.ActionLink(
    (item.AppCode as object).ToString(),
    "Index",
    "ApplicationHealthCheck",
    new { Code = item.AppCode },
    null),
Lutetium answered 27/7, 2011 at 18:58 Comment(0)
A
0

Try this

grid.Column("AmountValue", header: "Amount", format:@<text><input name="Amount" type="text" value="@item.AmountValue"</text>)
Authoritative answered 29/7, 2011 at 11:9 Comment(1)
i want to add dynamic textbox column with this bt it is not editable..ie i m unable to enter any value in this textboxPhyllous
P
0

Showing Link or plain text based upon a condition:

grid.Column(format: (dbItem) => dbItem.QtyCompleted == 0 ?
                 Html.ActionLink(
                 "Start Setup",
                 "SaveData", 
                 "Workorder",
                 new
                   {
                       msid = dbItem.MSID,
                       qtyCompleted = 0, 
                       serialNum = "ABC"
                   },
                 new 
                   { 
                       onclick = "return ConfirmAction('Start Setup');" 
                   }) :
                 Html.Label(((DateTime)dbItem.StartTime).ToShortDateString())
            )
Priapus answered 26/1, 2012 at 18:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.