oCcSking
oCcSking

Reputation: 928

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

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.

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

Upvotes: 0

Views: 1265

Answers (2)

moshe
moshe

Reputation: 26

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.

(I know that was two year before but maybee that can help)

Upvotes: 1

Matt Bodily
Matt Bodily

Reputation: 6423

I would associate an id with the transaction count and pass that back to the controller through an ajax call

$.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

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). Hopefully this helps.

Upvotes: 1

Related Questions