0

I have a from and to date search box on my view.

This POSTs to my controller, which then searches for available rooms between the dates selected. The results are then listed in the view again.

I'm used to WebForms, where you can PostBack, and grab any control data - however in MVC you can't do this.

When the results are displayed, how can I then POST back to a controller, both the RoomId that has been selected:

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

...and the tbFrom and tbTo dates from the TextBoxes?

My view is below.

Thanks for any assistance,

Mark

@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>
Mark
  • 7,778
  • 24
  • 89
  • 147

1 Answers1

0

make your views strongly typed... and to know what strongly typed views are in mvc.net here are a few links

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/

more over if you have the default routes set in your application, in your POST action result you can get the id as the route value like

[HttpPost]
public ActionResult POSTACTION(int id){
 //here you will get the room id 
}

but from the code what i see is that you are not posting a form, actionlinks make a GET request in that case remove the [HttpPost] action filter from the action result...

EDIT

may be i just misunderstood the question... in your current scenario if ajax is an option you can do something like this

assign a class to your action links like

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

now attach a click event handler to it

$(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();   
  });
});

specify the action name and controller to which the form will be posted

@using (Html.BeginForm("MyAction","ControllerName",FormMethod.POST)){...}

in your controller you will have something like

[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
}
Community
  • 1
  • 1
John x
  • 4,031
  • 8
  • 42
  • 67
  • Hi @John - thank you - I can get the ID no problem - it's how I also include the dteFrom and dteTo values I'm unsure about. I've read the links you kindly provided, but I'm still not clear on how, when I have a number of rooms listed, with unique IDs, to POST/GET the ID AND include the from and to dates from the text boxes. Thanks again, Mark – Mark Jul 07 '12 at 20:51
  • Hi @John - I haven't got it working yet, but can see where you're pointing me, and 80% understand it - I'll take what you've suggested, do some more searching, and I'm certain I'll get it to work over the next day or two. Thanks a lot for taking the time to get me on the right track, I really appreciate it. Cheers, Mark – Mark Jul 07 '12 at 23:05