我的视图上有一个开始和结束日期的搜索框。
此 POST 将发送到我的控制器,然后控制器在所选日期之间搜索可用房间。 然后,结果将再次列在视图中。
我习惯了WebForms,您可以在其中回发并获取任何控件数据 - 但是在MVC中您无法执行此操作。
显示结果后,我如何回发控制器,即已选择的 RoomId:
@Html.ActionLink("Book Room","Book", new { id=item.RoomId })
。以及文本框中的 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>
使您的视图强类型...要了解 mvc.net 中的强类型视图,这里有一些链接
http://www.howmvcworks.net/OnViews/BuildingAStronglyTypedView
什么是 MVC 中的强类型视图 ASP.NET
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})
现在将 Click 事件处理程序附加到它
$(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
}