如何在相应按钮单击的单个视图上呈现多个模型的数据

时间:2015-05-14 07:31:37

标签: javascript html asp.net-mvc razor asp.net-mvc-5

我使用的是mvc5,我创建了一个Model tableData.cs类,其中包含以下代码:

namespace WebApplication2.Controllers
{
    public class HomeController : Controller
    {
        // GET: Home

        public ActionResult tableData1()
        {
            ViewBag.NbColumns = 2;
            List<tableData1> tableDataModel = new List<tableData1>();
            tableDataModel.Add(new tableData1()
            {
                Name = "shekha",
                Location = "USA",               
                Time="12:00PM"
            });

            return View(tableDataModel.ToList());
        }    

    }
}

我对应的控制器是:

@model  IEnumerable<WebApplication2.Models.tableData1>
@{
    ViewBag.Title = "tableData1";
    Layout = null;
}

<div style="width:1300px;">
    <table id="activeDeviceTable" class="table table-striped table-bordered dataTable" cellspacing="0" cellpadding="0" border="0" aria-describedby="activeDeviceTable_info">
        <thead>
            <tr role="row">
                <th class="sorting" tabindex="0" rowspan="1" colspan="1" aria-label="Name: activate to sort column ascending">Name</th>
                <th class="sorting" tabindex="0" rowspan="1" colspan="1" aria-label="Location: activate to sort column ascending">Location</th>              
                <th class="sorting" tabindex="0" rowspan="1" colspan="1" aria-label="Time: activate to sort column ascending">Time</th>
            </tr>
        </thead>

        <tbody role="alert" aria-live="polite" aria-relevant="all">
            <tr>
                @foreach (var item in Model)
                {
                    <td class="someCssStyle">@item.Name</td>
                    <td class="someCssStyle">@item.Location</td>                   
                    <td class="someCssStyle">@item.Time</td>
                }
            </tr>
        </tbody>
    </table>
</div>

观点是:

function FirstButtonClick(el)
    {
        $('#tab1').bind('click', function ()
        {        
            var url = 'Home/tableData1';
            $.ajax({
                url: url,
                data: {}, //parameters go here in object literal form
                type: 'GET',
                datatype: 'json',
                success: function (data) { $('div#theNewView').html(data); },
                error: function () { alert('something bad happened in FirstButtonClick'); }
            });
            alert("hello1");
        });
        $('#tab1').trigger('click');
    }

我有一个div,我在按钮点击时使用javascript渲染此表,如下所示:

<div id="theNewView" style="float: left; margin: 5px 2px 2px 5px;">     
</div>

呈现此视图的div是:

 public ActionResult tableData2()
        {
            ViewBag.NbColumns = 2;
            List<tableData2> tableDataModel = new List<tableData2>();
            tableDataModel.Add(new tableData2()
            {
                Name = "ajeet",
                Location = "allahbad",               
                Time = "1:00PM"
            });
            return View(tableDataModel.ToList());
        }

现在我想做什么?   我想创建另一个具有相同字段的模型,比如tableData2.cs,在他的控制器中,我会这样做:

   public ActionResult tableData1()
        {
            ViewBag.NbColumns = 2;
            List<tableData1> tableDataModel = new List<tableData1>();
            tableDataModel.Add(new tableData1()
            {
                filterActiceDeviceList = true,
                filterIdleDeviceList = false,               
                Name = "shekha",
                Location = "USA",               
                Time = "12:00PM"
            });
            tableDataModel.Add(new tableData1()
            {
                filterActiceDeviceList=false,
                filterIdleDeviceList=true,
                Name = "ajeet",
                Location = "allahbad",             
                Time = "1:00PM"
            });           
            return View(tableDataModel);
        }

现在我希望这个控制器在另一个按钮点击时将tableData1.cshtml中tableData1的数据替换为tableData2控制器的数据。

我的意思是我只想保留一个包含表的视图(tableData1.cshtml),并且我希望将这个视图重用于我的所有模型类,如tableData1.cs和tableData2.cshtml。

有可能这样做吗?怎么做?

编辑:在stefen建议之后我将控制器代码更改为:

for

我添加了两个过滤器filterActiceDeviceList和filterIdleDeviceList,其中true必须显示tableData1.cshtml中与其对应的数据,但我无法理解我将如何编写代码以区分FirstButtonClick中的表数据()和SecondButtonClick()函数(在ajax调用中)基于这些标志,使每个按钮显示对应于它的真实值的数据。

例如在FirstButtonClick()中将只显示与filterActiceDeviceList = true对应的数据,在这种情况下,表格将包含Name:shekha,Location:USA,Time = 12:00 PM。

0 个答案:

没有答案