0

我的视图上有一个从开始到现在的搜索框。

这 POST 到我的控制器,然后搜索所选日期之间的可用房间。然后结果再次列在视图中。

我习惯了 WebForms,您可以在其中 PostBack 并获取任何控制数据 - 但是在 MVC 中您不能这样做。

显示结果后,我如何将已选择的 RoomId POST 回控制器:

 @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>
4

1 回答 1

0

使您的视图具有强类型...并且要知道 mvc.net 中的强类型视图是什么,这里有一些链接

http://www.howmvcworks.net/OnViews/BuildingASstronglyTypedView

什么是 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 
}

但从代码中我看到的是您没有发布表单,actionlinksGET在这种情况下发出请求,[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
}
于 2012-07-07T20:43:34.530 回答