【问题标题】:MVC get data from GridView to display on ViewMVC 从 GridView 获取数据以显示在 View 上
【发布时间】:2014-08-21 18:23:37
【问题描述】:

让我看看我是否可以更清楚地说明我在寻找什么。

我有一个视图“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");
}

所以当我看到视图时,“名称”字段的文本是“一些东西”..

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

【问题讨论】:

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


    【解决方案1】:

    您可以在操作中添加名称参数。如果我正确理解你在代码中所做的事情......

    [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 或其他无法检查的数字。

    【讨论】:

    • 我怎样才能将它合并到我的基础设施网格中?我的网格代码看起来像... column.For(x => x.Name).Template("${Name}")。 HeaderText("姓名").Width("10%");
    • 嗨 Omar,您可能希望将该代码包含在代码块中的问题中!很难在评论中阅读。考虑到这一点,您可以将?name=$[Name] 附加到您的href。
    • 很抱歉。谢谢!所以我试过了,现在它传递了这个名字。我需要对所有其他领域做同样的事情吗?还是像所说的那样传递 ID 会给出我需要的网格中的所有值?
    • 如果您有办法通过 id 获取数据,那么是的,Id 将是最好的。您也可以根据名称进行查找,但最好通过 id 进行查找,其中 id 是唯一的,并且名称并不总是唯一的。你将如何做这一切都取决于你如何存储数据。
    猜你喜欢
    • 2021-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多