2012-07-07 161 views
0

我在我的视图中有一个from和from date搜索框。将控制值传递给控制器​​

此POST发送给我的控制器,然后在我选择的日期之间搜索可用房间。结果然后再次在视图中列出。

我习惯了WebForms,在那里你可以PostBack,并抓住任何控制数据 - 但在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> 

回答

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 
} 
+0

喜@约翰 - 谢谢你 - 我能得到的ID没有问题 - 这是我怎么也包括dteFrom和dteTo值我不确定。我已经阅读了您提供的链接,但我仍然不清楚如何在列出多个房间时使用唯一ID对POST/GET ID进行操作,并在文本框中包含日期和日期。再次感谢您,Mark – Mark 2012-07-07 20:51:07

+0

嗨@John - 我还没有工作,但可以看到你指向我的位置,80%的人知道它 - 我会采取你的建议,做更多的搜索,并且我确信我会在第二天或者第二天内完成它的工作。非常感谢您花时间让我走上正轨,我非常感谢。干杯,马克 – Mark 2012-07-07 23:05:42

+0

很高兴我可以帮忙...':)' – 2012-07-08 05:18:01