繁体   English   中英

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

[英]How can I return a list from view to controller in MVC4

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

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

这是我的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); 
    }

现在在我的View上,我尝试从这个@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 =)

我会将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。 希望这会有所帮助。

你可以简单地发送SIMsales类似条件sellerId您的视图模型和剃须刀隐藏字段,以保持数据和pthe后功能(控制器上)只retrive所有这些SIMsales您在获取FUNC计数。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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