查看未呈现MVC 4 [英] View not rendering MVC 4
问题描述
我有,当我一步通过code我看到它到控制器的动作,并返回看法,但它不会加载它只是停留在当前页面上的观点并没有被称为视图。
当点击一个特定的页面的ajax的职位是由呼叫下一个页面,这并不加载上的一个按钮。
我的位指示动作看起来是这样的:
公众的ActionResult比较(INT [] Y)
{
CostModel.Getsuff(Y); 返回查看(CostModel);
}
我的路线图是这样的:
routes.MapRoute(
名称:比较,
网址:类别/比较/ {* XXXX},
默认:新{控制器=类别,行动=比较}
).RouteHandler=新CategoriesRouteHandler();公共类CategoriesRouteHandler:IRouteHandler
{
公众的IHttpHandler GetHttpHandler(RequestContext的的RequestContext)
{
IRouteHandler处理程序=新MvcRouteHandler();
VAR瓦尔斯= requestContext.RouteData.Values;
。VAR列表=丘壑[XXXX]的ToString()斯普利特('');
INT [] Y =新INT [List.length的数字]
的for(int i = 0; I< List.length的数字,我++)
int.TryParse(名单[I]的ToString(),出值Y [i]); vals.Add(Y,y)基
返回handler.GetHttpHandler(RequestContext的);
}
}
我的AJAX
$阿贾克斯({
键入:POST,
网址:。'@ Url.Action(比较,类别,新{类别=占位符})'代替(占位符,ARR)
数据类型:JSON
传统:真实,
成功:函数(MSG){警报(MSG)}
});
正如我提到的previously当我跟踪它时,它执行Ajax调用罚款和命中它只是不启动视图控制器上的行动。我想这可能是与该地区不被任何定义的事实呢?因此,也许它扔出去,看不到MVC 4随时随地指定不同的领域,但我是新来的MVC。任何帮助将AP preciated。
正如我提到的previously当我跟踪它时,它执行Ajax调用
罚款和命中它只是不启动控制器上的作用
视图。
块引用>这就是AJAX =>停留在同一页上的整点。如果要替换当前页面不使用任何AJAX。使用普通的
Html.ActionLink
。如果你的控制器操作只返回
PartialView
,你想只刷新你的页面的一部分,你可以做,在成功
回调:$。阿贾克斯({
键入:POST,
网址:。'@ Url.Action(比较,类别,新{类别=占位符})'代替(占位符,ARR)
传统:真实,
成功:函数(结果){
$('#someIdToRefresh')HTML(结果);
}
});另请注意,我已删除
数据类型:'JSON'
,因为你的控制器动作不返回任何JSON。它返回的text / html
。I have a view which is not being called when I step through the code I see it get to the action in the controller and return the view but it does not load the view it just stays on the current page.
When a button is clicked on a specific page an ajax post is made calling the next page this does not load.
My contoller action looks like this:
public ActionResult Comparison(int[] y) { CostModel.Getsuff(y); return View(CostModel); }
My route map looks like this:
routes.MapRoute( name: "Compare", url: "Category/Comparison/{*xxxx}", defaults: new { Controller = "Category", Action = "Comparison" } ).RouteHandler = new CategoriesRouteHandler(); public class CategoriesRouteHandler : IRouteHandler { public IHttpHandler GetHttpHandler(RequestContext requestContext) { IRouteHandler handler = new MvcRouteHandler(); var vals = requestContext.RouteData.Values; var list = vals["xxxx"].ToString().Split(','); int[] y= new int[list.Length]; for (int i=0; i < list.Length; i++) int.TryParse(list[i].ToString(),out y[i]); vals.Add("y", y); return handler.GetHttpHandler(requestContext); } }
My ajax
$.ajax({ type: "POST", url: '@Url.Action("Comparison", "Category", new { Categories = "PLACEHOLDER"})'.replace('PLACEHOLDER', arr), dataType: "json", traditional: true, success: function(msg){alert(msg)} } );
As I mentioned previously when I trace it, it performs the ajax call fine and hits the action on the controller it just does not launch the view. I thought it may have something to do with the fact that the area is not defined anywhere? So perhaps its throwing it out, can't see anywhere in MVC 4 to specify different area, but I am new to MVC. Any help will be appreciated.
解决方案As I mentioned previously when I trace it, it performs the ajax call fine and hits the action on the controller it just does not launch the view.
That's the whole point of AJAX => stay on the same page. If you want to replace the current page don't use any AJAX. Use a normal
Html.ActionLink
.And if your controller action only returns a
PartialView
and you would like to refresh only a portion of your page, you could do that in thesuccess
callback:$.ajax({ type: "POST", url: '@Url.Action("Comparison", "Category", new { Categories = "PLACEHOLDER"})'.replace('PLACEHOLDER', arr), traditional: true, success: function(result) { $('#someIdToRefresh').html(result); } });
Also notice that I have removed
dataType: 'json'
because your controller action is not returning any JSON. It's returningtext/html
.这篇关于查看未呈现MVC 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!