1

有人可以帮助我如何使用 jQuery 和 ajax 将模型发布回控制器。

当我发布表单时,我的控制器收到一个空模型。请纠正我在哪里做错了。

模型:

public class AllocateToStore
    {
        public IList<OrderLine> FailureAllocations { get; set; }
        public IList<SelectListItem> AllocationStatus
        {
            get
            {
                // code to fetch list.
            }
        }
    }

 public class OrderLine
    {
        public long Id { get; set; }
        public DateTime Date { get; set; }
        public int Status { get; set; }
    }

控制器:

public ActionResult AutoAllocate()
        {
// This action will load the view with data.
// Get model data and send it to view.

            return View("Allocated",model);
        }

        [HttpPost]
        public ActionResult ResolveUnallocatedOrders(AllocateToStore coll)
        {
// When user changes the selection in grid and post the page I need to get the selection // here. So that I can update that record.
            return null;
        }

观点是

 @model AllocateToStore
    @{
        ViewBag.Title = "Orders";
    }
    @{
        var grid = new WebGrid(Model.FailureAllocations, rowsPerPage: 100);
    }


        if (Model.FailureAllocations.Any())
        {
     <form>
        <div>
            @grid.GetHtml(
                    columns: grid.Columns(
                        grid.Column(columnName: "Order date", header: "Order Date", format: item => item.Order.Date),
                       grid.Column("dropdown", header: "Resolution", format:
                                                                   @<span>
                                                                       @{ var index = Guid.NewGuid().ToString(); }
                                                                       @Html.Hidden("FailureAllocations.Index", index)
                                                                       @Html.Hidden("FailureAllocations[" + index + "].Id", (long)item.Id)
                                                                       @Html.DropDownList("FailureAllocations[" + index + "].Status", new SelectList(Model.AllocationStatus, "Value", "Text", item.Status))
                                                                   </span>
                     )
                    ),
                    tableStyle: "expandable-table",
                    htmlAttributes: new { id = "gridFailureAllocations" }
                )
            <br />
            <input type="submit" value="Resolve" id="resolve-button" />
        </div>
</form>
        }


  @section scripts
{
    <script>
        $("#resolve-button").click(function () {
            debugger;
            alert("here");
            $.ajax({
                url: '/OrderProcessing/ResolveUnallocatedOrders',
                data: $('#form').serialize(),
                type: 'POST'
            });
        });
    </script>
} 

谢谢,纳雷什

4

2 回答 2

0

我没有测试这个答案。这只是建议。请尝试这种方式。

  $.ajax({
      url: o.url,
      type: 'post',
      contentType: "application/x-www-form-urlencoded",
      data: {"FailureAllocations ":JSON.stringify(FailureAllocations), "AllocationStatus":JSON.stringify(AllocationStatus)}',
       .  . . . 
     });
于 2012-10-29T17:20:30.063 回答
0

我认为你这里有一个错误data: $('#form').serialize(),

$('#form')将选择 id 为“form”的所有元素。您的表单没有 id,因此您的选择器将无法正常工作。尝试将该行更改为data: $('form').serialize(),,选择器应该可以工作。

或者,给你的表单一个“form”的id,例如<form id="form">,原来的选择器$('#form')应该可以工作。

有关各种 jQuery 选择器的更多详细信息,请参见此处

于 2012-10-29T23:07:45.717 回答