简体   繁体   English

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

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

I like to do one simple control for day transactions. 我喜欢为日间交易做一个简单的控制。 That need to show me how much transaction we have this day (close or open trans) and when the manager click the number of transaction I would like to send all this list of transactions into a nice table. 这需要向我展示我们今天有多少交易(关闭或开放交易)以及当经理点击交易数量时我想将所有交易清单发送到一个漂亮的表中。 But I can't find the way to do it. 但我找不到办法。 Not even across the web. 甚至没有在网上。

here is my try but nothing. 这是我的尝试,但没有。 I am open for suggestion =) 我愿意接受建议=)

Here is my ViewModel 这是我的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>();
    }
}

the Controller look like I fill all the data and I sent it to view 控制器看起来像我填写所有数据,我发送它来查看

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

Now on my View I try to send this list from this @Html.ActionLink like we can see here. 现在在我的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>

The problem is that the view/controller who supposed to get this list is getting an empty list. 问题是谁应该得到这个列表的视图/控制器得到一个空列表。

10Q =) 10Q =)

I would associate an id with the transaction count and pass that back to the controller through an ajax call 我会将id与事务计数相关联,并通过ajax调用将其传递回控制器

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

}); });

create a partial view for displaying the table and on your controller populate and return the partial view using the passed id 创建一个用于显示表的局部视图,并在控制器上填充并使用传递的id返回局部视图

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

then put a div on your view with a class of Content (or whatever you want to call it). 然后使用一类内容(或任何你想要的内容)在你的视图上放置一个div。 Hopefully this helps. 希望这会有所帮助。

You can simply sent the SIMsales condition like sellerId on your ViewModel and a Hidden field on Razor to keep the data and on pthe post function (on your controller) just retrive all of those SIMsales that you count on the Get func. 你可以简单地发送SIMsales类似条件sellerId您的视图模型和剃须刀隐藏字段,以保持数据和pthe后功能(控制器上)只retrive所有这些SIMsales您在获取FUNC计数。

(I know that was two year before but maybee that can help) (我知道那是两年前,但也许可以提供帮助)

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

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