简体   繁体   中英

MVC get data from GridView to display on View

Let me see if I can make it more clear of what im looking for.

I have a view 'UpdateLead'

on the GET in my controller I have..

[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");
}

SO when I see the View, the 'name' field has text "some stuff"..

but what i want is basically to get that 'name' information from a gridview that is on another view called 'ViewLeads'. The gridview is an Infragistics grid. So basically if the user selects the 3rd user in the grid, I want to return all the data for that user (user ID 3). Im very new to MVC and I'm totally lost right now. Thanks!

You could add a parameter of name to the action. If I understand what you're doing in your code correctly....

[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");

}

To do this by 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");

}

You could then use Html.ActionLink in your gridview

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

Regarding your comment: Html.ActionLink generates the link for you. If you wanted to incorporate this manually, you could try something like this:

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

Edit I just noticed that you mentioned (user ID 3) . You can do the same thing by passing an integer. You can either have it nullable and check the value, or default it to 0 or some other number that it could never be to check against.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM