2017-06-19 38 views
0

这是我的控制器的操作方法错误而从视图中的数据传递给控制器​​

[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 

/> 

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

回答

0

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

public ActionResult UpdateOrder(dsmanager.Models.State model) 

则动作在里面可以用

model.state_country_id 
访问属性
相关问题