如何在MVC4中将视图中的列表返回到控制器

时间:2013-07-13 21:31:18

标签: c# asp.net-mvc-4

我喜欢为日间交易做一个简单的控制。 这需要向我展示我们今天有多少交易(关闭或开放交易)以及当经理点击交易数量时我想将所有交易清单发送到一个漂亮的表中。 但我找不到办法。甚至没有通过网络。

这是我的尝试,但没有。 我愿意接受建议=)

这是我的 ViewModel

public class SIMcontrolsViewModel
{

    public DTControls DTransactionControl { get; set; }
}  
public class DTControls
{
    public DateTime TDate { get; set; }

    public int NumOfTransaction { get; set; }
    public List<SIMsale> SIMsaleList { get; set; }

    public DTControls()
    {
        SIMsaleList = new List<SIMsale>();
    }
}

控制器看起来像我填写所有数据并将其发送到视图

    [AdminAuthorization]
    public ActionResult DateTransactionsControl(DateTime? date)
    {
        SIMcontrolsViewModel vm = new SIMcontrolsViewModel();
        vm.DTransactionControl = new DTControls();
        if (!date.HasValue)
            date = DateTime.Now;//.Today;
        vm.DTransactionControl.TDate = date.Value;
        try
        {
            using (CompanyContext db = new CompanyContext())
            {
                var saleLst = db.SIMsales.ToList();

                foreach (var sale in saleLst)
                {
                    if (..)
                        vm.DTransactionControl.SIMsaleList.Add(sale);

                }
                var tCount = vm.DTransactionControl.SIMsaleList.Count;
                vm.DTransactionControl.NumOfTransaction = tCount;
            }
        }
        catch (Exception ex)
        {..}
        return View(vm); 
    }

现在,在查看上,我尝试从此@Html.ActionLink发送此列表,就像我们在此处看到的那样。

@model oCc.IPToGo.ViewModel.SIMcontrolsViewModel


<fieldset>
      <table border="0" class="display">
            <thead>
                <tr>            
                   <th style="width:100px">Date</th>
            <th style="width:100px">@Html.DisplayNameFor(model => model.DTransactionControl.NumOfTransaction)</th>                
        </tr>
    </thead>
    <tbody>
        <tr style="text-align: center">
            <td>@Model.DTransactionControl.TDate.ToShortDateString()</td>
            @if (Model.DTransactionControl.NumOfTransaction != 0)
            {
                <td>
                    @Html.ActionLink(Model.DTransactionControl.NumOfTransaction.ToString(), "../SIMsale/",
                                                    new { sell = Model.DTransactionControl.SIMsaleList },
                                                    new { style = "font-weight: bold; color: Highlight" })
                </td>      

            }
            else
            {
                <td style="color:red">0</td>
            }
        </tr>
    </tbody>
</table>
 </fieldset>

问题是应该获取此列表的视图/控制器获取空列表。

10Q =)

2 个答案:

答案 0 :(得分:1)

我会将id与事务计数相关联,并通过ajax调用将其传递回控制器

$.ajax({
 url: "@(Url.Action("Action", "Controller"))",
 type: "POST",
 cache: false,
 async: true,
 data: { id: 'associatedID' },
 success: function (result) {
     $(".Content").html(result);
 }

});

创建一个用于显示表的局部视图,并在控制器上填充并使用传递的id返回部分视图

public PartialViewResult BuildTable(int id){
    Model model = (get results from database for id)
    return PartialView("_Partial", model);
}

然后在您的视图中使用一类内容(或任何您想要调用它)的div。希望这会有所帮助。

答案 1 :(得分:1)

您只需在ViewModel上发送SIMsales sellerId条件,在Razor上发送隐藏字段即可保存数据和post函数(在控制器上)只检索所有这些SIMsales条件{{1}} 1}}你依靠Get func。

(我知道那是两年前,但也许可以提供帮助)