1

I am trying to apply T4MVC to my project. Say, I have an ajax search box, it calls Home/SearchQuery action which takes in a string q as parameter. How do I write that line in T4MVC?

From Ajax.BeginForm("SearchQuery", "Home", .... To Ajax.BeginForm(MVC.Home.SearchQuery(???)...

.cshtml file

@using (Ajax.BeginForm("SearchQuery", "Home", /* <-----Convert to T4MVC Here */
        new AjaxOptions {
            LoadingElementId = "loadingGif",
            OnSuccess = "parseResults",
            OnFailure = "searchFailed"
        })) {
    <input type="text" name="q" />
    <input type="submit" value="Search" />
    <img id="loadingGif" style="display:none" src="@Url.Content("~/content/images/loading.gif")" />
}

<div id="searchResults" style="display: table"></div>
tereško
  • 58,060
  • 25
  • 98
  • 150
Tom
  • 15,781
  • 14
  • 69
  • 111

2 Answers2

2

Your q is submitted from the input in form, so you could just write

@using (Ajax.BeginForm(MVC.Home.SearchQuery(),
        new AjaxOptions {
            LoadingElementId = "loadingGif",
            OnSuccess = "parseResults",
            OnFailure = "searchFailed"
        })) {
    <input type="text" name="q" />
    <input type="submit" value="Search" />
    <img id="loadingGif" style="display:none" src="@Url.Content("~/content/images/loading.gif")" />
}
archil
  • 39,013
  • 7
  • 65
  • 82
  • I don't get what you mean. Because MVC.Home.SearchQuery() will give you a syntax error, the SearchQuery signature has parameter (string q), right? like this ... [OutputCache(NoStore = true, VaryByParam = "", Duration = 0)] [AcceptVerbs(HttpVerbs.Post)] public virtual JsonResult SearchQuery(string q) { if (!Request.IsAjaxRequest()) return null; ....// more code – Tom May 05 '12 at 17:00
  • That signature mismatching syntax error (what to fill into the string q) is exactly where my problem is. – Tom May 05 '12 at 17:08
  • 1
    T4MVC always generates an overload with no parameters, so what @archil suggests should work. Have you tried? – David Ebbo May 05 '12 at 17:10
  • Interesting! I just realised it does for the ones that return ActionResult or JsonResult, but the ones that return an string doesn't have that overload function you are talking about. Why is that? – Tom May 05 '12 at 17:17
  • You probably need to regenerate your T4MVC. That way, T4MVC will add overload that does not have parameters, and your code is going to work – archil May 05 '12 at 17:31
  • 2
    Action methods that don't return an ActionResult (or something that extends that) are not supported by T4MVC. It would be difficult based on how it works. – David Ebbo May 05 '12 at 17:33
  • Thank you very much for your answer David and archil. I will create a view for the string to work around the problem. – Tom May 05 '12 at 17:41
  • 1
    You don't need to create a view just because you want to return a string. See http://stackoverflow.com/questions/553936/in-mvc-how-do-i-return-a-string-result for a better way to deal with this. – David Ebbo May 06 '12 at 03:39
1

Another possible answer: regenerate the template

I know it's a bit stupid, but I got here just because I forgot to regenerate the classes with the template (the new method with parameters is accessible before you regenerate the templates). Maybe someone will find this usefull.

Guillaume86
  • 14,341
  • 4
  • 53
  • 53