 |
BOOK: Beginning ASP.NET Web Pages with WebMatrix  | This is the forum to discuss the Wrox book Beginning ASP.NET Web Pages with WebMatrix by Mike Brind, Imar Spaanjaars ; ISBN: 978-1-1180-5048-4 |
|
Welcome to the p2p.wrox.com Forums.
You are currently viewing the BOOK: Beginning ASP.NET Web Pages with WebMatrix section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

November 3rd, 2014, 05:36 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Are you sure your Helpers code file is correct and in App_Code. This should work, unless Helpers is broken, or in the wrong place.
I do get errors on this code though:
if (HttpContext.Current.Request["title"].Length > 200)
when there is no title. You should wrap that code inside the IsPost check.
Imar
|

November 3rd, 2014, 06:07 PM
|
Registered User
|
|
Join Date: Nov 2014
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Helpers
Yes, I have added Helpers, under the folder App_Code:
@helper Selected(string option, string value){
if(HttpContext.Current.Request[option] !=null){
var values = HttpContext.Current.Request[option].Split(',');
if(values.Contains(value)){
<text>selected=\"selected\"</text>
}
}
}
@helper Checked(string option, string value){
if(HttpContext.Current.Request[option] !=null){
var values = HttpContext.Current.Request[option].Split(',');
if(values.Contains(value)){
<text>checked=\"checked\"</text>
}
}
}
These functions do work, as I ran it in another example (following the book here). But when I try to use it in my main project, the errors pop up (context errors). I'm rather new to all of this; what have I missed?
|

November 3rd, 2014, 07:28 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
>> I'm rather new to all of this; what have I missed?
I don't know yet. This should just work.
Can you post the exact error message and a description of when you get it? (In VS, in the browser on load or after a postback and so on)?
Imar
|

November 3rd, 2014, 07:29 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Also, did you correct the other error I mentioned? Finally, can you post the full code for the page as you have it now?
Imar
|

November 3rd, 2014, 07:32 PM
|
Registered User
|
|
Join Date: Nov 2014
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Errors
Server Error in '/' Application.
Compilation Error
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 'Helpers' does not exist in the current context
Source Error:
Line 55: <select name="duration">
Line 56: <option value="">--Choose one</option>
Line 57: <option value="1" @Helpers.Selected("Duration", "1")>1 Day</option>
Line 58: <option value="3" @Helpers.Selected("Duration", "3")>3 Days</option>
Line 59: <option value="7" @Helpers.Selected("Duration", "7")>7 Days</option>
Source File: c:\Users\dabbilus\Documents\My Web Sites\Kastla\Sell.cshtml Line: 57
Show Detailed Compiler Output:
Show Complete Compilation Source:
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.34212
This is the error I get when I run the program.
|

November 3rd, 2014, 08:09 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
What's the file name of the helpers code in App_Data?
Also, can you post your current code as per my previous post?
Imar
|

November 3rd, 2014, 09:28 PM
|
Registered User
|
|
Join Date: Nov 2014
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Code
Page.Title = "Post Your Advertisement";
if(IsPost){
if(HttpContext.Current.Request["title"].IsEmpty()){
ModelState.AddError("title", "Please provide a title");}
}
if(HttpContext.Current.Request["title"].Length > 200){
ModelState.AddError("title", "Your title cannot exceed 200 characters");
}
if(HttpContext.Current.Request["description"].IsEmpty()){
ModelState.AddError("description", "You must provide a description");
}
if(HttpContext.Current.Request["duration"].IsEmpty()){
ModelState.AddError("duration", "Please choose a duration");
}
if(HttpContext.Current.Request["price"].IsEmpty()){
ModelState.AddError("price", "You must provide price");
}
if(!HttpContext.Current.Request["price"].IsDecimal() && !HttpContext.Current.Request["price"].IsEmpty()){
ModelState.AddError("price", "Provide a valid number for the price");
}
if(HttpContext.Current.Request["condition"].IsEmpty()){
ModelState.AddError("condition", "State the condition for your item");
}
if(HttpContext.Current.Request["email"].IsEmpty()){
ModelState.AddError("email", "You must provide a valid email");
}
if(!ModelState.IsValid){
ModelState.AddFormError("Please fix the errors");
}
}
<body>
<form id="post-advert" action="@Href("~/Sell")" method="post" >
<fieldset>
<legend>Post Your Advertisement</legend>
@Html.ValidationSummary(true)
<div>
<label for="title">Title</label>
</div>
<div>
<input type="text" name="title" value="@Request["title"]" />
@Html.ValidationMessage("title")
</div>
<div>
<label for="description">Description</label>
</div>
<div>
<textarea name="description">@Request["description"]</textarea>
@Html.ValidationMessage("description")
</div>
<div>
<label for="duration">Duration</label>
</div>
<div>
<select name="duration">
<option value="">--Choose one</option>
<option value="1" @Helpers.Selected("Duration", "1")>1 Day</option>
<option value="3" @Helpers.Selected("Duration", "3")>3 Days</option>
<option value="7" @Helpers.Selected("Duration", "7")>7 Days</option>
<option value="14" @Helpers.Selected("Duration", "14")>14 Days</option>
</select>
@Html.ValidationMessage("duration")
</div>
<div>
<label for="price">Price</label>
</div>
<div>
<input type="text" name="price" value="@Request["price"]"/>
@Html.ValidationMessage("price")
</div>
<div>
<label for="condition">Price</label>
</div>
<div>
<input type="radio" name="condition" value="@Request["condition"]"
@Helpers.Checked("condition","Fair")/> Fair
<input type="radio" name="condition" value="Good"
@Helpers.Checked("condition", "Good")/>Good
<input type="radio" name="condition" value="As New"
@Helpers.Checked("condition", "As New")/>As New
@Html.ValidationMessage("condition")
</div>
<div>
<label for="email">Your Email Address</label>
</div>
<div>
<input type="text" name="email" value="@Request["email"]"/>
@Html.ValidationMessage("email")
</div>
<div>
<input type="submit" name="submit" value="Post" />
</div>
</fieldset>
</form>
</body>
@section footer{
This is a footer for the Sell page
}/>
Helpers.cshtml is the Form name under App_Code Folder.
Functions.cshtml is the form for Functions
@using System.Text.RegularExpressions;
@functions{
public static bool IsValidEmail(string s){
string pattern = @"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$";
return Regex.IsMatch(s, pattern);
}
}
I know that this will help: Intellisense doesn't recognize the two functions in Helpers. There was 4 methods under Intellisense, and neither Selected and Checked was not there ( BrowserHelper was one of them). Should I make a Using statement for ASP.Web Net? MVC is not using it now.
|

November 4th, 2014, 01:45 AM
|
Wrox Author
|
|
Join Date: Dec 2011
Posts: 57
Thanks: 1
Thanked 19 Times in 19 Posts
|
|
Can you post the code that's in Helpers.cshtml?
|

November 4th, 2014, 08:31 AM
|
Registered User
|
|
Join Date: Nov 2014
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Helpers code
@helper Selected(string option, string value){
if(HttpContext.Current.Request[option] !=null){
var values = HttpContext.Current.Request[option].Split(',');
if(values.Contains(value)){
<text>selected=\"selected\"</text>
}
}
}
@helper Checked(string option, string value){
if(HttpContext.Current.Request[option] !=null){
var values = HttpContext.Current.Request[option].Split(',');
if(values.Contains(value)){
<text>checked=\"checked\"</text>
}
}
}
There you go. There is only the two functions.
Like you, I have authored seven books (Trafford here in the USA and Ãrn og Ãrlygur publishers in Iceland). In the 90's I authored Easy VB (I was a Systems Analyst) in Iceland and in 2005 I had a massive stroke which left me totally unable to read, write and speak for almost five years. I had to retrain myself to where I could do languages (English and Icelandic), but unfortunately, I had lost my computer skills (I worked on the FCAT Team in Florida and used programming in C# with ASP). Since I have not worked ever since the stroke, I just wanted to see if I could still do the work. It is frustrating to say the least, so please bear with me.
Snæbjörn
|

November 4th, 2014, 09:36 AM
|
Wrox Author
|
|
Join Date: Dec 2011
Posts: 57
Thanks: 1
Thanked 19 Times in 19 Posts
|
|
Your error messages have me confused. There must be something else going on that we cannot see. I suggest you download the code for the chapter and compare it to what you have there - both content and structure. Make sure the chapter code runs first. The latest version of WebMatrix may prompt you to upgrade to a more recent version of Web Pages. You should say no to that invitation for the time being.
|
|
 |