0

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?

6
  • Those are two different files. Commented Jun 29, 2013 at 20:52
  • 1
    That code doesn't belong in a view, should be in controller or elsewhere really. Commented Jun 29, 2013 at 20:53
  • 1
    Maybe it is related to the line where you are building the url. You should be using double quotes instead of single quotes. ' + strSearch + ' should be " + strSearch + ". Commented Jun 29, 2013 at 21:01
  • @chue, no. Even I remove this line, it is still wrong. I guess it might be in a controller. Commented Jun 29, 2013 at 21:03
  • @Love - Perhaps unrelated, but it still looks like a bug to me. Commented Jun 29, 2013 at 21:08

1 Answer 1

2

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));
        }
    };
 }
Sign up to request clarification or add additional context in comments.

6 Comments

Okay, if I move it to a controller. How it is related with a specific ActionResult?
@Love, since GetPassage returns a plain string, the closest ActionResult method is Content. However I doubt the very idea of turning this method into action method. It is hard to advice what should be done with this method without knowing scenarios of its use.
I want to make an ajax call, the sample is at blog.bobcravens.com/2009/11/…. The example has var url = "/Monitor/Test/ReverseTheString"; I am not sure what is /Monitor then?
@Love, most likely Monitor is an Area. If you have no areas definedm then you can safely leave it out, having just "/Test/ReverseTheString"
could you please look at my update? Suppose the controller class is HomeController.cs.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.