C# 从JavaScript调用视图
我有一个JavaScript函数定义如下:C# 从JavaScript调用视图,c#,javascript,jquery,asp.net-mvc,C#,Javascript,Jquery,Asp.net Mvc,我有一个JavaScript函数定义如下: function onQuickSearchClick(s) { var query = s.GetText(); $.post('/Search/', { query: query }); } public ActionResult Index(string query) { return View((object)query); } 现在我想用SearchController中的查询文本调用视图“Search”。我该怎
function onQuickSearchClick(s) {
var query = s.GetText();
$.post('/Search/', { query: query });
}
public ActionResult Index(string query)
{
return View((object)query);
}
现在我想用SearchController中的查询文本调用视图“Search”。我该怎么做
执行此操作时,不显示任何视图:
SearchController.cs:
public ActionResult Index(string query)
{
// Can't do "return View(query)" here, because this would be interpreted as the view name
return View();
}
如何将查询参数传递给我的视图/Search/Index.cshtml?为此,必须使用三个参数重载 第一个参数是视图名称,第二个参数是主视图名称,最后一个参数是模型
return View("Index", "", query);
为此,您必须使用三参数重载 第一个参数是视图名称,第二个参数是主视图名称,最后一个参数是模型
return View("Index", "", query);
我想我不明白。您可以将字符串作为模型返回,如下所示:
function onQuickSearchClick(s) {
var query = s.GetText();
$.post('/Search/', { query: query });
}
public ActionResult Index(string query)
{
return View((object)query);
}
然后,您的MVC将知道query
是一个模型,而不是viewName
我想我不明白。您可以将字符串作为模型返回,如下所示:
function onQuickSearchClick(s) {
var query = s.GetText();
$.post('/Search/', { query: query });
}
public ActionResult Index(string query)
{
return View((object)query);
}
然后您的MVC将知道
query
是一个模型,而不是viewName$.post('/Search?query='+query)代码>将查询
字符串强制转换为对象<代码>返回视图((对象)查询)代码>$.post('/Search?query='+query)代码>将查询
字符串强制转换为对象<代码>返回视图((对象)查询)代码>这不会将我重定向到“搜索”视图的“索引”视图以显示结果,是吗?@SeToY您可以将第一个参数更改为您想要的任何视图,我只是猜测了您想要返回的视图。谢谢,这只是关于window.location='@Url.Action(“索引”,“搜索”)
AliRıza Adıyahş我设法弄明白了。不过,我投票支持你们两个,因为你们的“三参数重载”也需要完成,而不是强制转换到对象(object
:)谢谢!这不会将我重定向到“搜索”视图的“索引”视图以显示结果,是吗?@SeToY您可以将第一个参数更改为您想要的任何视图,我只是猜测了您想要返回的视图。谢谢,这只是关于window.location='@Url.Action(“索引”,“搜索”)
AliRıza Adıyahş我设法弄明白了。不过,我投票支持你们两个,因为你们的“三参数重载”也需要完成,而不是强制转换到对象(object
:)谢谢!你很明白。我缺少的是window.location='@Url.Action(“索引”、“搜索”)
的重定向。我认为这可以通过在ActionResult
中指定视图来实现。显然,我错了。你很明白。我缺少的是window.location='@Url.Action(“索引”、“搜索”)
的重定向。我认为这可以通过在ActionResult
中指定视图来实现。显然,我错了。