我有一个非常简单的观点。它包含一些C#代码。
@{
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));
}
}
}我不知道是怎么回事。错误信息是:
Source Error:
Line 117:EndContext("~/Views/Home/Community.cshtml", 236, 9, true);更新:
如果我把代码移到控制器上。
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));
}
}我想要一个基于例句的ajax调用。javascript中的代码如何?
发布于 2013-06-29 21:04:10
视图不是声明方法的合适位置。实际上,在@{和}之间的视图中编写的所有代码都是在同一个方法中运行的(不完全正确,但要说明要点)。显然,在C#中不可能在另一个方法中声明一个方法,视图引擎只是没有足够的方法来将它逐字逐句地翻译过来。
但是,如果您需要在视图上使用一些实用程序方法--您可以创建一个委托,然后调用它:
@{
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));
}
};
}https://stackoverflow.com/questions/17384543
复制相似问题