Click here to Skip to main content
15,896,500 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hii to all,
I have a requirement that i need to get complete rows data of a table from view to controller and then display in another different view, The actual scenario is i have a table where i need to display the employees data like Empid,Ename,Task,in addition to this columns i have placed one more field called Edit(Link Field),so that with the help of Edit link field we can update employee details, here what i need is whenever i clicked on EditLink i need to redirect to another page say editable page,in this editable page i need to get the respective rows(the row editlink is fired)data and dispaly in the textbox's in an editable mode, so that he can modify the employee details and click on submit buttton then changes should reflect to the table. Here i iam not using any database for displaying table data, in the view itself i designed the table & populated the data to the table fields,please guide me how can i pass complete rows data from one View to another different view..thanks in advance

Thanks
Ramu
Posted
Comments
Jameel VM 1-May-13 10:44am    
did you try anything?
Jameel VM 1-May-13 10:45am    
Please post the code of listed table view and controller.
Ram7 from Hyderabad 2-May-13 0:37am    
Dear Moideen,Thanks for ur intrest towards my post,here is my controller code
public ActionResult Login(UsersLogin model)
{
if (model.UserName == "employee1" && model.Password == "ramu")
{
//return View("~/Views/Account/Register");
return View("employee1");
}
else if (model.UserName == "employee2" && model.Password == "ramu2")
{
return View("employee2");
}
else if (model.UserName == "manager" && model.Password == "manager")
{
return View("manager");
}
else if (model.UserName == "admin" && model.Password == "admin")
{
return View("admin");
}

else
{
ViewData["Error"] = "Invalid UserId or Password";
return View();
}

}
I Have created 5 views
1)Normal Login View:-

Member Login


type="text/javascript"></script>
@Html.ValidationSummary(true, "Login was unsuccessful. Please correct the errors and try again.")
@using (Html.BeginForm()) {
<div>
<fieldset>
@* Account Information
*@
<div class="editor-label">
@Html.LabelFor(m => m.UserName)
</div>
<div class="editor-field">
@Html.TextBoxFor(m => m.UserName)
@Html.ValidationMessageFor(m => m.UserName)
</div>

<div class="editor-label">
@Html.LabelFor(m => m.Password)
</div>
<div class="editor-field">
@Html.PasswordFor(m => m.Password)
@Html.ValidationMessageFor(m => m.Password)
</div>

@*<div class="editor-label">
@Html.CheckBoxFor(m => m.RememberMe)
@Html.Label(ViewData["Error"])
</div>*@
<div>
@ViewData["Error"]
</div>
<p>
<input type="submit" value="Log On" />
</p>
</fieldset>
</div>
}
2)employee1 View:-
<table>
<tr>
<th></th>
<th>Empid</th>
<th>Ename</th>
<th>Task</th>
</tr>
<tr>
<td>@Html.ActionLink("Edit","Edit")
</td>
<td>101</td>
<td>Ramu</td>
<td>Fixing Bugs</td>
</tr>
</table>
3)Similarly employee2 view:-

You are Logged in as employee2



<table>
<tr>
<th></th>
<th>Empid</th>
<th>Ename</th>
<th>Task</th>
</tr>
<tr>
<td>@Html.ActionLink("Edit","Edit")</td>
<td>102</td>
<td>Vijay</td>
<td>Testing Code</td>
</tr>
</table>
4)Manager view:-

You are Logged in as Manager



<table>
<tr>
<th></th>
<th>Empid</th>
<th>Ename</th>
<th>Task</th>
</tr>
<tr>
<td>@Html.ActionLink("Edit","Edit")
<td>101</td>
<td>Ramu</td>
<td>Fixing Bugs</td>
</tr>
<tr>
<td>102</td>
<td>Vijay</td>
<td>Testing Code</td>
</tr>
</table>
5)Admin view:-
<table>
<tr>
<th.</th>
<th>Employees</th>
<th>ManagerName</th>
<th>ManagerTask</th>
</tr>
<tr>
<td>@Html.ActionLink("Edit","Edit")
<td>Ramu</td>
<td>RamaKrishna</td>
<td>Code Review</td>
</tr>
<tr>
<td>Vijay</td>
<td>Naresh</td>
<td>Code Review</td>
</tr>
</table>
Hear i need to create one
Jameel VM 2-May-13 11:48am    
Hi friend..i have posted a small example whatever you expect for u ..please follow this and change the business logic that you want..Hope this helps

1 solution

You don't need to create a separate views for this. I have create a sample application whatever you expect. Please follow the steps
Step1 : Create a model under the Model folder like this
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MvcApplication1.Models
{
    public class MyDataViewModel
    {
        public string Selection { get; set; }
        public List<Manager> ManagersData{get;set;}
        public List<EmployeeOne> EmployeeOneData { get; set; }
        public List<EmployeeTwo> EmployeeTwoData { get; set; }
    }
    public class Manager 
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
     public class EmployeeOne 
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
     public class EmployeeTwo 
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
}

Step2: Create three Partial Views under shared folder for manager,Employee1,Employee2,etc like below
EmployeeOne Partial View
C#
@model IEnumerable<mvcapplication1.models.employeeone>
@{
    ViewBag.Title = "_employeeOne";
}
<h2>_employeeOne</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Name)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Name)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
            @Html.ActionLink("Details", "Details", new { id=item.Id }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.Id })
        </td>
    </tr>
}
</table></mvcapplication1.models.employeeone>

Manager Partial View
C#
@model IEnumerable<mvcapplication1.models.manager>
<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Name)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Name)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
            @Html.ActionLink("Details", "Details", new { id=item.Id }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.Id })
        </td>
    </tr>
}
</table></mvcapplication1.models.manager>

Create other partial views like which return the IEnumerable collection or whatever your collection format
Step3: Create Controller(SelectViewController) and ActionResult(Index) like below
C#
public class SelectViewController : Controller
   {
       public ActionResult Index()
       {
//based on your login logic set the data to the collection property of MyDataViewModel class and return this model to the view
           var model = new MyDataViewModel();
           string yourLoginStatus = "EmployeeOne";
           if (yourLoginStatus == "Manager")
           {
               model.ManagersData = ManagerData();
//Set the selection status here
               model.Selection = "Manager";
           }
           else if (yourLoginStatus == "EmployeeOne")
           {
               model.EmployeeOneData = EmployeeOneData();
//Set the selection status here
               model.Selection = "EmployeeOne";
           }
           else if (yourLoginStatus == "EmployeeTwo")
           {
               model.EmployeeTwoData = EmployeeTwoData();
//Set the selection status here
               model.Selection = "EmployeeTwo";
           }
           return View(model);
       }
//this method loads the manager  data
       public List<Manager> ManagerData()
       {
           var manager = new List<Manager>();
           manager.Add(new Manager{Id=1,Name="Manager1"});
            manager.Add(new Manager{Id=2,Name="Manager2"});
           return manager;
       }
//this method loads the employee one data
       public List<EmployeeOne> EmployeeOneData()
       {
           var employee = new List<EmployeeOne>();
           employee.Add(new EmployeeOne { Id = 1, Name = "Emp1" });
           employee.Add(new EmployeeOne { Id = 2, Name = "Emp2" });
           return employee;
       }
//this method loads the employee two data
       public List<EmployeeTwo> EmployeeTwoData()
       {
           var employee = new List<EmployeeTwo>();
           employee.Add(new EmployeeTwo { Id = 1, Name = "EmpTwo1" });
           employee.Add(new EmployeeTwo { Id = 2, Name = "EmpTwo2" });
           return employee;
       }
   }


Step 4 :Finally Create a View by right clicking the Index action

C#
@model MvcApplication1.Models.MyDataViewModel
@{
    ViewBag.Title = "Index";
}
//Based on the status that we are setting inside the index action load the partial views
@if (Model.Selection == "Manager")
{
     Html.RenderPartial("_manager", Model.ManagersData);
}
else if (Model.Selection == "EmployeeOne")
{
     Html.RenderPartial("_employeeOne", Model.EmployeeOneData);
}
else if (Model.Selection == "EmployeeTwo")
{
     Html.RenderPartial("_employeeTwo", Model.EmployeeTwoData);
}
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900