如何将表的整行数据从一个视图传递到另一个不同的视图MVC3 [英] How to pass entire rows data of a table from one view to another different view MVC3

查看:54
本文介绍了如何将表的整行数据从一个视图传递到另一个不同的视图MVC3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hii to all,

我有一个要求,我需要从视图到控制器获取表的完整行数据,然后在另一个不同的视图中显示,实际场景是我有一个表我需要显示员工数据,如Empid,Ename,Task,除了这个列,我还放了一个名为Edit(Link Field)的字段,这样在Edit链接字段的帮助下我们可以更新员工的详细信息,这里是什么我需要的是每当我点击EditLink我需要重定向到另一个页面说可编辑页面,在这个可编辑的页面中我需要获取相应的行(行editlink被触发)数据和显示在文本框中的可编辑模式,这样他就可以修改员工的详细信息并点击提交按钮然后更改应该反映到表中。在这里,我没有使用任何数据库来显示表数据,在视图中我设计了表格&填充数据到表字段,请指导我如何将完整的行数据从一个视图传递到另一个不同的视图..提前谢谢



谢谢

Ramu

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

推荐答案

您无需为此创建单独的视图。无论你期望什么,我都会创建一个示例应用程请按照以下步骤进行操作

步骤1:在Model文件夹下创建一个这样的模型

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
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; }
    }
}



步骤2:在管理员,Employee1,Employee2等共享文件夹下创建三个部分视图,如下所示

EmployeeOne Partial View


Step2: Create three Partial Views under shared folder for manager,Employee1,Employee2,etc like below
EmployeeOne Partial View

@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

@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>



创建其他部分视图,例如返回IEnumerable集合或任何收集格式

Step3:创建Controller(SelectViewController)和ActionResult(Index)之类的下面


Create other partial views like which return the IEnumerable collection or whatever your collection format
Step3: Create Controller(SelectViewController) and ActionResult(Index) like below

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;
       }
   }





步骤4:最后通过右键单击索引操作创建视图





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

@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);
}


这篇关于如何将表的整行数据从一个视图传递到另一个不同的视图MVC3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆