将数据从视图传递到控制器时出错

时间:2017-06-19 06:13:38

标签: razor model-view-controller

这是我的控制器操作方法

[HttpPost]
        public ActionResult UpdateOrder(List<State> objState)
        {
            DataTable dst_state = new DataTable();
            dst_state.Columns.Add("state_id", typeof(int));
            dst_state.Columns.Add("state_order_by", typeof(int));
            foreach (var abc in objState)
            {
                DataRow dr = dst_state.NewRow();
                dr["state_id"] = abc.state_id;
                dr["state_order_by"] = abc.state_order;
                dst_state.Rows.Add(dr);
            }
            State.UpdateOrderState(dst_state);
            TempData["message"] = "Order Updated Successfully";
            return View("ViewState");
        }

这是我的观点

    @model dsmanager.Models.State
    @using PagedList.Mvc;
    @using dsmanager.Controllers;
    @using dsmanager.DLL;

    @using (Html.BeginForm("UpdateOrder", "State", FormMethod.Post, new { enctype = "multipart/form-data" }))
    {
        Html.AntiForgeryToken();
        Html.EditorForModel();
        ViewBag.Title = "View State";

        @*<script type="text/javascript">
            function GetList() {
                var active = "0"
                var inactive = "0"
                $("#loading").show();

                var param = { active: active, inactive: inactive }
                $.ajax({
                    type: 'POST',
                    dataType: 'html',
                    url: '/State/GetStateData',
                    data: param,
                    success: function (Data) {
                        $("#loading").hide();
                        $("#divpartialTable").empty();
                        $("#divpartialTable").html(Data);
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        $("#loading").hide();
                    }
                });
            }
            </script>*@

        <section class="content-header">
            <h1>State Details</h1>
            <ol class="breadcrumb">
                <li>
                    <a href="@Url.Action("InsertState", "State")" class="btn btn-danger"><i class="fa fa-plus">&nbsp;Add</i></a>
                </li>
            </ol>
        </section>
        <div>@Html.DropDownListFor(model => model.state_country_id, Model.Country, "Please Select", new { onchange = "GetList();", @class = "form-control" }  ) </div>
            <br />
            <div>@Html.TextBoxFor(model => model.state_name ,  new { @class = "form-control" })</div>
            <br />
        @*<a id="btnSearch"  href="@Url.Action("ViewState", "State")" class="btn btn-danger"><i class="fa fa-plus">&nbsp;Search</i></a>*@
        <div class="screen-small">
            <section class="content">
                <div id="divgrid">
                    <div id="Addgrid" class="row">
                        <div class="col-xs-12">
                            <div class="box box-primary">
                                <div class="box-header with-border">

                                </div>
                                <div class="box-body no-padding">
                                    <div>

                                    </div>
                                    <div class="resp-table">
                                        <div>
                                            <table class="table table-striped" cellspacing="0" cellpadding="0" style="border-width:0px;width:100%;border-collapse:collapse">
                                                <thead>
                                                    <tr class="th" style="white-space:nowrap;">
                                                        <th scope="col">Edit</th>
                                                        <th scope="col">ID</th>
                                                        <th class="text-left" align="left" scope="col">State Name</th>
                                                        <th class="text-left" align="left" scope="col">Country</th>
                                                        <th class="text-left" align="left" scope="col">Status</th>
                                                        <th class="text-left" align="left" scope="col">Order</th>
                                                    </tr>
                                                </thead>
                                                <tbody>
                                                    @for (int i = 0; i < Model.pagedlist.Count; i++)
                                                    {
                                                        <tr valign="top">
                                                            <td class="text-center" width="20px" data-title="Edit"><a href="@Url.Action("EditState", "State", new { id = Model.pagedlist[i].state_id.ToString() }  )"><i class="fa fa-pencil" title="edit"></i>E</a></td>
                                                            <td class="text-center" width="20px" data-title="ID">@Html.DisplayFor(model => model.pagedlist[i].state_id)</td>
                                                            <td class="text-center" width="100px" style="text-align:left" data-title="ID">@Html.DisplayFor(model => model.pagedlist[i].state_name)</td>
                                                            <td class="text-center" width="100px" style="text-align:left" data-title="ID">@Html.DisplayFor(model => model.pagedlist[i].country_name)</td>
                                                            <td class="text-center" width="100px" style="text-align:left"> @(Html.DisplayFor(model => model.pagedlist[i].state_status).ToString() == "1" ? "Active" : "Inactive")</td>
                                                            <td class="text-center" width="100px" style="text-align:left" data-title="ID">@Html.TextBoxFor(model => model.pagedlist[i].state_order)</td>
                                                            <td style="display:none">@Html.HiddenFor(model => model.pagedlist[i].state_id)</td>
                                                        </tr>
                                                    }
                                                </tbody>
                                                <tr class="th" style="white-space:nowrap;">
                                                    <td width="20px"></td>
                                                    <td width="20px"></td>
                                                    <td width="100px"></td>
                                                    <td width="100px"></td>
                                                    <td width="100px"></td>
                                                    <td class="text-left" width="100px">
                                                        <input type="submit" value="Update Order" class="btn btn-default" />
                                                    </td>
                                                </tr>
                                            </table>
                                        </div>
                                    </div>
                                </div>

                                <div class="box-footer text-right">
                                    @*Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount*@
                                    @Html.PagedListPager(Model.pagedlist, page => Url.Action("ViewState", new { page }))
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

            </section>

        </div>
    }
    <br 

/>

在将数据从视图传递到控制器以更新订单列时,我在控制器中获取空列表。如何读取控制器中的两个字段以更新顺序。请帮忙。

1 个答案:

答案 0 :(得分:0)

控制器操作的参数不是您的模型,应该是:

public ActionResult UpdateOrder(dsmanager.Models.State model)

然后在操作中,您可以使用

访问属性
model.state_country_id