MVC从GridView获取数据以在View上显示

时间:2014-08-21 18:23:38

标签: asp.net-mvc gridview infragistics ignite-ui iggrid

让我看看我能否更清楚地知道我在寻找什么。

我有一个'UpdateLead'视图

在我的控制器的GET上我有......

[HttpGet]
public ActionResult UpdateLead()
{
    LoginUser user = Session["User"] as LoginUser;
    if (user != null)
    {
         BusinessLead bl = new BusinessLead();
         bl.Name = "some stuff";

         return View(bl1);
    }
    return RedirectToAction("Login", "Main");
}

所以当我看到View时,'name'字段有文字“some stuff”..

但我想要的基本上是从另一个名为'ViewLeads'的视图中的gridview获取'name'信息。 gridview是一个Infragistics网格。所以基本上如果用户选择网格中的第三个用户,我想返回该用户的所有数据(用户ID 3)。我对MVC很新,我现在完全迷失了。谢谢!

1 个答案:

答案 0 :(得分:0)

您可以为操作添加name参数。如果我理解你在代码中正在做什么......

[HttpGet]
public ActionResult UpdateLead(String name = "")
{
    if (!String.IsNullOrEmpty(name))
    {
        LoginUser user = name as LoginUser;
        BusinessLead bl = new BusinessLead();
        bl.Name = "some stuff";

        return View(bl1);
    }
    if (user != null)
    {
        LoginUser user = Session["User"] as LoginUser;
        BusinessLead bl = new BusinessLead();
        bl.Name = "some stuff";

        return View(bl1);
    }
    return RedirectToAction("Login", "Main");

}

通过Id执行此操作:

[HttpGet]
public ActionResult UpdateLead(Int32 UserId = -1)
{
    LoginUser user = Session["User"] as LoginUser;
    if (UserId > -1)
    {
        BusinessLead bl = new BusinessLead();
        bl.Name = "some stuff";
        bl = GetUserInfoById(UserId);    // Some method you need to make to populate your BusinessLead class based on the id field
        return View(bl1);
    }
    if (user != null)
    {
        BusinessLead bl = new BusinessLead();
        bl.Name = "some stuff";

        return View(bl1);
    }
    return RedirectToAction("Login", "Main");

}

然后,您可以在gridview中使用Html.ActionLink

@Html.ActionLink(UserName, "UpdateLead" "ControllerName", new {name=UserName}, null)

关于您的评论:Html.ActionLink会为您生成链接。如果你想手动合并,你可以尝试这样的事情:

column.For(x => x.Name).Template("<a href='UpdateLead?name=${Name]'style='color:blue;'>${Name}</a>").HeaderText("Name").Width("10%‌​");

修改我刚才注意到您提到了(user ID 3)。你可以通过传递一个整数来做同样的事情。您可以将其置为可空并检查该值,或将其默认为0或其他一些永远无法检查的数字。