我正在使用mvcContrib生成网格,以允许用户通过键入搜索数据来过滤数据。在我的索引视图中呈现了几个局部视图:
以下是处理搜索的部分视图:
@model CRMNPS.Models.PagedViewModel<CRMNPS.Models.NPSProcessed>
@using (Html.BeginForm("Index", "Home", FormMethod.Get))
{
<label>
Model Number: @Html.TextBox("searchWord" )
<br /><br />From Date: @Html.EditorFor(m => m.FromDate)
</label>
<label>
<Br /><br />To Date: @Html.EditorFor(m => m.ToDate)
</label>
<label>
<br /><br /> <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传递带有搜索条件的viewmodel和mvcContrib使用的IQueryable对象。
当用户按下导出到Excel按钮时,我想将所选值传递回Index actionresult HttpPost控制器。 (FromDate,ToDate和SearchWord)
FromDate,ToDate和SearchWord值始终返回null。
我对MVC很新,所以欢迎提出任何建设性意见。
由于
乔
答案 0 :(得分:4)
由于它们不是您要发布的形式 - (导出到Excel是单独的形式)。 输入
FromDate,ToDate和SearchWord
是第一种形式(在局部视图中)。所以这些值不会出现在控制器中(因为它们不是http帖子的一部分)。 如果你想看到所有这些值被传递回控制器,它们应该在一个
之下Html.BeginForm
答案 1 :(得分:1)
一种方法是将所有内容放入与MoXplod建议的相同的形式,或者您可以使用一些javascript通过挂钩第二个表单的提交事件来发送搜索值作为查询字符串,如
$('#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将这些值发布到控制器而不是完整的常规回发。