views:

130

answers:

3

I have an asp page containing a form with two fields where the client is supposed to write his/her information.

<td><%=Html.TextBox("Date")%></td>
<td><%=Html.TextBox("City")%></td>

My action's signature looks (at the moment) like this:

public ActionResult XXX(string Date, string City) {...}

The problem is that I want to propagate a simple integral number from my view to the action. Is it possible to have a hidden textbox or something similar? I want my action signature to look something like this:

public ActionResult CreateCourseDate(string Date, string City, int id)
+1  A: 

Yes, a hidden input will work for that. It has to be on the same form, obviously.

Use Html.Hidden.

Craig Stuntz
+2  A: 

You can pass additional values like this:

<%  using (Html.BeginForm("YourActionName", "YourControllerName", new { id = yourid })) { %>
...
<td><%=Html.TextBox("Date")%></td>
<td><%=Html.TextBox("City")%></td>
...
<input type="submit" value="Submit" />
<% } %>
David Tischler
+4  A: 

There are two ways to do this. Either use the hidden field method (I wouldn't recommend this) or add it to the url (route value). The latter fits better with the REST-like urls that MVC prefers. So, if you are using an new form, then your url would look like:

http://example.com/course/newcoursedate/1

This would edit the course with id 1. You'd have a corresponding create action that your new form would POST to. The example below assumes that your view is strongly typed with a Course model. You could also pull the id out of ViewData if necessary.

<% using (Html.BeginForm( "CreateCourseDate",
                          "Course",
                          new { id = ViewData.Model.CourseID }, ...

    ...

    <td><%=Html.TextBox("Date")%></td>
    <td><%=Html.TextBox("City")%></td>

<% } %>

I didn't look at the order of the parameters, but I believe that at least one signature has the route values after the controller name. Consult the source (or intellisense) to be sure.

tvanfosson