0

ビューに開始日と終了日の検索ボックスがあります。

これは私のコントローラーにPOSTし、コントローラーは選択した日付の間に利用可能な部屋を検索します。その後、結果が再びビューに一覧表示されます。

私はWebFormsに慣れており、PostBackを実行して、任意の制御データを取得できますが、MVCではこれを実行できません。

結果が表示されたら、選択したRoomIdの両方をコントローラーにPOSTバックするにはどうすればよいですか。

 @Html.ActionLink("Book Room","Book", new { id=item.RoomId })

...そして、TextBoxからの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/BuildingAStronglyTypedView

ASP.NETMVCで強く型付けされたビューとは

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
}
于 2012-07-07T20:43:34.530 に答える