带有RadioButton的WebGrid MVC 3获得选中

时间:2012-06-21 05:31:17

标签: asp.net-mvc-3 webgrid

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

enter image description here

祝福

2 个答案:

答案 0 :(得分:0)

试试这个

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

另请查看此链接http://fiddle.jshell.net/Gt4GH/

这不是已编译的代码,请尝试此操作并让我知道它是否有效:)

答案 1 :(得分: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>
}