2012-07-07 160 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