1

如何使 WebGrid 显示在图片中并使用 rariobutton 获取选定的行

在此处输入图像描述

祝福

4

2 回答 2

0

尝试这个

 myGrid.Column(header: "Select", format: @<text><input name="chck" 
  TYPE="RADIO" CHECKED="@item.select" /></text>),

还要检查这个链接http://fiddle.jshell.net/Gt4GH/

这不是编译代码,试试这个,让我知道它是否有效:)

于 2012-06-21T06:14:01.357 回答
0

您可以定义视图模型:

public class UserViewModel
{
    public int Id { get; set; }
    public string Email { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

public class MyViewModel
{
    public int? SelectedUserId { get; set; }
    public IEnumerable<UserViewModel> Users { get; set; }
}

然后是控制器:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var users = Enumerable.Range(1, 5).Select(x => new UserViewModel
        {
            Id = x,
            Email = "email " + x,
            FirstName = "fn " + x,
            LastName = "ln " + x,
        });
        var model = new MyViewModel
        {
            Users = users
        };
        return View(model);
    }

    [HttpPost]
    public ActionResult Index(int? selectedUserId)
    {
        return Content(string.Format("Thank you for selecting user id: {0}", selectedUserId));
    }
}

最后是一个观点:

@model MyViewModel

@{
    var grid = new WebGrid(Model.Users);
}

@using (Html.BeginForm())
{
    @grid.GetHtml(
        columns: grid.Columns(
            grid.Column(
                header: "Select", 
                format: @<text>@Html.RadioButtonFor(x => x.SelectedUserId, (int)item.Id)</text>
            ),
            grid.Column("Email"),
            grid.Column("FirstName", "First Name"),
            grid.Column("LastName", "Last Name")
        )
    )
    <button type="submit">OK</button>
}
于 2012-06-21T06:20:59.260 回答