繁体   English   中英

mvc3将数据从View传递到Controller时出现问题

[英]mvc3 Problem passing data from View to Controller

我正在使用mvcContrib生成网格,以允许用户通过键入搜索数据来过滤数据。 我的索引视图中呈现了几个局部视图:

这是处理搜索的局部视图:

@model CRMNPS.Models.PagedViewModel<CRMNPS.Models.NPSProcessed>
@using (Html.BeginForm("Index", "Home", FormMethod.Get))
{

    <label>
Model Number:&nbsp;&nbsp; @Html.TextBox("searchWord" )
<br /><br />From Date:&nbsp;&nbsp;&nbsp; @Html.EditorFor(m => m.FromDate)
</label>
<label>
<Br /><br />To Date:&nbsp;&nbsp;&nbsp; @Html.EditorFor(m => m.ToDate)
</label>    
<label>
<br /><br />&nbsp;&nbsp;<input class="button" value="Search" type="submit" />
<br />
</label>

}

这是我的索引视图:

@model PagedViewModel <CRMNPS.Models.NPSProcessed>

@{
    ViewBag.Title = "CRM Processed List";
    Layout = "~/Views/Shared/_Layout.cshtml";
}


<h2>Processed List</h2>
@{Html.RenderPartial("SearchBox");}
@{Html.RenderPartial("Pager", Model.PagedList);}
@Html.Grid(Model.PagedList).AutoGenerateColumns().Columns(column =>{
column.For(x => Html.ActionQueryLink(x.ModelNumber, "Edit", new { id = x.Id
})).Named("Id").InsertAt(1);
}).Sort(Model.GridSortOptions).Attributes(@class => "grid-style")

@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { FromDate = Model.FromDate, ToDate = Model.ToDate, SearchWord = Model.SearchWord }))
{
   <p>
       <input class="button" value="Export to Excel" type="submit" />
   </p>
}    

在索引视图的底部,我在Html.BeginForm中还有一个带有Formmethod.Post的提交。

调用此视图的Index ActionResult传递一个带有搜索条件的视图模型和一个mvcContrib使用的IQueryable对象。

当用户按下“导出到Excel”按钮时,我想将选定的值传递回Index actionresult HttpPost控制器。 (FromDate,ToDate和SearchWord)

FromDate,ToDate和SearchWord值始终返回null。

我对MVC相当陌生,因此欢迎提出任何建设性意见。

谢谢

由于它们不是您要发布的形式-(导出到Excel的形式是单独的)。 输入

FromDate,ToDate和SearchWord

处于第一种形式(在局部视图中)。 因此,这些值不会显示在控制器中(因为它们不属于http帖子)。 如果要查看所有这些值都传递回控制器,则这些值应小于1

Html.BeginForm

一种方法是将所有内容都放入与MoXplod建议的相同形式,或者您可以使用某些javascript,通过钩住第二种形式的Submit事件来将搜索值作为查询字符串发送

$('#excel-form').live('click', function(){
   var action = this.action;
   var searchString = $('#SearchWord').val();
   var toDateString = $('#ToDate').val();
   var fromDateString = $('#FromDate').val();
   if(action.indexOf('?')<0)
   {
      action = action+"?SearchWord="+searchString;
   }
   else
  {
     action = action+"&SearchWord="+searchString;
  }
  action = action + "&ToDate="+toDateString + "&FromDate=" + fromDateString;
  $(this).attr('action', action);
  return true;

});

它将这些值放在querystring中并使它们在action方法中可用。 或者,您可以使用ajax将这些值发布到控制器,而不是完整的常规发布。

暂无
暂无

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

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