我的视图中有一个来自最新的搜索框。
此POST发送给我的控制器,然后控制器在所选日期之间搜索可用房间。然后,结果再次列在视图中。
我习惯了WebForms,你可以在PostBack中获取任何控制数据 - 但是在MVC中你不能这样做。
显示结果时,如何将POST返回控制器,同时选择RoomId:
@Html.ActionLink("Book Room","Book", new { id=item.RoomId })
...以及来自TextBoxes的tbFrom和tbTo日期?
我的观点如下。
感谢您的帮助,
标记
@model IEnumerable<ttp.Models.Room>
@{
ViewBag.Title = "Avail";
}
<h2>Avail</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
@using (Html.BeginForm())
{
<p>
Availability between @Html.TextBox( "dteFrom" , String.Format( "{0:dd/MM/yyyy}", DateTime.Now) , new { @class = "datepicker span2" } )
and @Html.TextBox( "dteTo" , String.Format( "{0:dd/MM/yyyy}", DateTime.Now) , new { @class = "datepicker span2" } )
<input type="submit" value="Search" /></p>
}
<table class="table table-striped table-bordered table-condensed" style="width:90%" id="indexTable" >
<tr>
<th>
@Html.DisplayNameFor(model => model.RoomName)
</th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.RoomName)
</td>
...
...
<td>
@Html.ActionLink("Book Room","Book", new { id=item.RoomId })
</td>
</tr>
}
</table>
答案 0 :(得分:0)
让你的观点强烈输入...并且知道mvc.net中强烈输入的观点是什么,这里有一些链接
http://www.howmvcworks.net/OnViews/BuildingAStronglyTypedView
What is strongly-typed View in ASP.NET MVC
http://blog.stevensanderson.com/2008/02/21/aspnet-mvc-making-strongly-typed-viewpages-more-easily/
如果你的应用程序中设置了默认路由,那么在POST
操作结果中,你可以获得id
作为路由值,如
[HttpPost]
public ActionResult POSTACTION(int id){
//here you will get the room id
}
但从代码中我看到的是您没有发布表单,actionlinks在这种情况下发出GET
请求,从动作结果中移除[HttpPost]
操作过滤器...
修改
可能是我误解了这个问题...在你当前的情况下如果ajax
是一个选项,你可以做这样的事情
为您的操作链接指定课程,例如
@Html.ActionLink("Book Room","Book", new { id=item.RoomId },new{@class="roomclass",id=item.RoomId})
现在附加一个点击事件处理程序
$(function(){
$(".roomclass").on("click",function(e){
e.preventDefault(); //prevent the default behaviour of the link
var $roomid = $(this).attr("id");//get the room id here
//now append the room id using hidden field to the form and post this form
//i will assume that you have only one form on the page
$("<input/>",{type:'hidden',name="RoomId",value:$roomid}).appendTo("form");
$("form").submit();
});
});
指定要将表单发布到的操作名称和控制器
@using (Html.BeginForm("MyAction","ControllerName",FormMethod.POST)){...}
在你的控制器中你会有类似的东西
[HttpPost]
public ActionResult MyAction(int RoomId,DateTime? dteFrom ,DateTime? dteTo ){
//you will get the form values here along with the room id
// i am not sure about the type `DateTime?` parameteres if this doesn't work try using string dteFrom and string dteTo
}