views:

115

answers:

3

Hi,

I'm trying to keep the entered data in the form after submitting by using "value=..."

I'm getting a compilation error on the following code:

<form id="myform">
           <input id="hour" type="text" name="hour" value="<%=hour%>" style="width:30px; text-align:center;" /> :
           <input id="minute" type="text" name="minute" value="<%=minute%>" style="width:30px; text-align:center;" />
           <br/>
           <input type="submit" value="Validate!" />
</form>

the error is : Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS0103: The name 'hour' does not exist in the current context

any solution? thanks a lot in advance, Lina

+1  A: 

Just add these lines of code and it should work.

<%

string hour = string.IsNullOrEmpty(Request.Form["hour"]) ? string.Empty : Request.Form("hour").ToString();

%>

<input id="hour" type="text" name="hour" value="<%=hour%>" style="width:30px; text-align:center;" />

The problem is that when you use <%=hour%>, it will look for a variable named hour and write its value there but since there's no such variable defines as hour before its used, it gives this error. What you actually want to do is to read the value of hour in case of page-submission via HTTP-POST and put it there as default value. The code above will just do that.

Moreover, I recommend you use ASP.NET Server Controls like () that provide this feature by default. These controls retain their values across postback and make them state-ful to an extent.

this. __curious_geek
not really, as "Form" is a property and not a method, i'm getting an error on it
Lina
code corrected.
this. __curious_geek
+1  A: 

Did you try putting runat="server" on your <form /> tag?

Munim Abdul
yes, but non of the above solutions is working!
Lina
Hey - see that the input is not aspx controls ! - He using the hour as variable, without declare it.
Aristos
+1  A: 

In an ASP.NET MVC application it is recommended to use html helpers to generate forms and input tags. For example:

Model:

public class MyModel
{
    public int Hour { get; set; } 
    public int Minute { get; set; } 
}

Controller:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View(new MyModel());
    }

    [HttpPost]
    public ActionResult About(MyModel model)
    {
        if (ModelState.IsValid) 
        {
            // the model is valid => do something with it
        }
        return View(model);
    }
}

View page:

<%@ Page Language="C#" 
         MasterPageFile="~/Views/Shared/Site.Master"
         Inherits="System.Web.Mvc.ViewPage<Ns.MyModel>" %>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

<% using (Html.BeginForm("index", "home", FormMethod.Post, new { id = "myform" })) %>
    <%= Html.TextBoxFor(x => x.Hour, new { style = "width:30px; text-align:center;" }) %>
    <%= Html.TextBoxFor(x => x.Minute, new { style = "width:30px; text-align:center;" }) %>
    <br/>
    <input type="submit" value="Validate!" />
<% } %>

</asp:Content>
Darin Dimitrov
that's a nice solution Darin, it works fine, but i'm trying to build a javascript based component that can be integrated with asp.net mvc2
Lina