Reputation: 1871
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
Upvotes: 1
Views: 4344
Reputation: 79
Action for submit must have parameter which have name like name of control on view or a model have property like name of control.
public ActionResult acTest(string hour, string minute)
{ ... }
Or
public ActionResult acTest(iTime md)
{ ... }
public class iTime
{
public string hour{get;set;}
public string minute{get;set;}
}
Upvotes: 1
Reputation: 43207
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.
Upvotes: 2
Reputation: 1038710
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>
Upvotes: 1