Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
540 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...