Reputation:
I have a view which is very simple. It contains some C# code.
@{
ViewBag.Title = "Community";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<div id="req1">
<form>
<input id="txt1" type="text" name="txt1" />
</form>
</div>
<div id="btn1">Send</div>
<div id="res1"></div>
@{
public string GetPassage(string strSearch)
{
using (var c = new System.Net.WebClient())
{
string url = "http://www.esvapi.org/v2/rest/passageQuery?key=IP&passage=' + strSearch + '&options=include-passage-references=true";
return c.DownloadString(Server.UrlDecode(url));
}
}
}
I don't know what is wrong. The error message is:
Source Error:
Line 117:EndContext("~/Views/Home/Community.cshtml", 236, 9, true);
Update:
If I moved the code to controller.
public ActionResult Community()
{
ViewBag.Message = "";
return View();
}
public string GetPassage(string strSearch)
{
using (var c = new System.Net.WebClient())
{
string url = "http://www.esvapi.org/v2/rest/passageQuery?key=IP&passage=" + strSearch + "&options=include-passage-references=true";
return c.DownloadString(Server.UrlDecode(url));
}
}
And I want to make a ajax call based on the example. How is the code in javascript?
Upvotes: 0
Views: 13567
Reputation: 56716
View is not the right place to declare a method. In fact all code that you write in a View between @{
and }
is run within the same method (not entirely true, but makes the point). Obviously declaration of a method inside another method is impossible in C#, view engine just does not have enough means to translate it to you literally.
However if you need some utility method on a view - you can create a delegate and call it later:
@{
ViewBag.Title = "Community";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<div id="req1">
<form>
<input id="txt1" type="text" name="txt1" />
</form>
</div>
<div id="btn1">Send</div>
<div id="res1"></div>
@{
Func<string, string> getPassge = strSearch =>
{
using (var c = new System.Net.WebClient())
{
string url = "http://www.esvapi.org/v2/rest/passageQuery?key=IP&passage=' + strSearch + '&options=include-passage-references=true";
return c.DownloadString(Server.UrlDecode(url));
}
};
}
Upvotes: 2