mvc3 将数据从视图传递到控制器时出现问题
我正在使用 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 传递带有搜索条件的视图模型和 mvcContrib 使用的 IQueryable 对象。
当用户按下“导出到 Excel”按钮时,我想将所选值传递回 Index actionresult HttpPost 控制器。 (FromDate、ToDate 和 SearchWord)
FromDate、ToDate 和 SearchWord 值始终返回 null。
我对 MVC 还很陌生,所以欢迎任何建设性的意见。
谢谢
乔
I am using mvcContrib to generate a grid to allow users to filter data by keying in search data. There are several partial views that are rendered in my Index View:
Here is the partial view that handles the searching:
@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>
}
Here is my Index view:
@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>
}
At the bottom of the Index View I have another submit within the Html.BeginForm with a Formmethod.Post.
The Index ActionResult that calls this view passes a viewmodel with the search criteria and a IQueryable object that the mvcContrib uses.
When the user presses the Export to Excel push button I would like to pass the selected values back to the Index actionresult HttpPost controller. (FromDate, ToDate and SearchWord)
The FromDate, ToDate and SearchWord values always come back null.
I am fairly new to MVC so any constructive comments are welcome.
Thanks
Joe
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因为它们不是您发布的表单 - (导出到 Excel 采用单独的表单)。
输入
均采用第一种形式(在部分视图中)。因此这些值不会显示在控制器中(因为它们不是 http post 的一部分)。
如果您想看到所有这些值被传递回控制器,它们应该在 1 之下
Since they are not in the form that you are posting - (Export to Excel is in a separate form).
The inputs
Are in the first form (in the partial view). So those values don't show up in the controller (since they are not part of the http post).
If you want to see all these values being passed back to the controller, they should be under one
一种方法是将它们全部采用 MoXplod 建议的相同形式,或者您可以使用一些 javascript 通过挂钩第二种形式的提交事件来将搜索值作为查询字符串发送,就像
它将这些值放入查询字符串中并使它们在操作中可用一样方法。或者,您可以使用 ajax 将这些值发布到控制器,而不是完全常规回发。
One way is to put'em all in the same form as MoXplod suggested or you can use some javascript to send search values as query string by hooking the submit event of second form like
it will put these values in querystring and make them available in action method. Alternatively, you can use ajax to post these values to controller rather than full regular post back.