2015-12-17 44 views
2

我试试这个代码: -ASP.NET MVC重定向到操作不会呈现最终查看

如果没有查询字符串提供给索引方法,然后呈现分支定位器视图。在该视图中选择分支标识后,请回发到重定向路径结果或操作结果方法,然后使用所选分支标识的查询字符串重定向回索引。

我可以在没有查询字符串的情况下成功运行代码。 我什至运行索引视图,可以看到模型正常工作,但索引视图不呈现,分支选择器视图仍然存在。在执行重定向时,网络开发人员工具会正确显示正确的URL和查询字符串。

(注意:两种方法都在同一个控制器上)。

如果我直接在浏览器地址栏中添加相同的查询字符串,它工作正常!

我有这样的代码:

[HttpGet] 
public ActionResult Index() 
{ 
    var querystringbranchId = Request.QueryString["branchId"]; 

    if(!string.IsNullOrEmpty(querystringId)) 
    { 
     ....do stuff like build a model using the branchId... 

     return View(Model); 
    } 

    return View("BranchSelector") 
} 

[HttpPost] 
public RedirectToRouteResult BranchDetails(FormCollection formCollection) 
{ 
    var querystringBranchId = formCollection["BranchList"]; 
    var branchId = int.Parse(querystringBranchId); 

    return RedirectToAction("Index", new { branchId }); 
} 
+0

你能分享你的索引视图代码吗? – Dilip

+0

索引代码仅将Querystring解析为INT,然后使用返回模型的服务。谢谢! – AlwaysLearning

+2

AlwaysLearning:这与经典ASP无关,请重新提出您的问题。 – Paul

回答

2

尝试在帖子上使用强类型模型,并将param指定为实际参数 - 使用View模型对您来说会更好。

我已经测试过下面的 - 它似乎按预期工作对我来说:

[HttpGet] 
public ActionResult Index(int? branchId) 
{ 
    if (branchId.HasValue) 
    { 
     return View(branchId); 
    } 

    return View("BranchSelector"); 
} 

[HttpPost] 
public RedirectToRouteResult BranchDetails(MyModel myModel) 
{ 
    return RedirectToAction("Index", new { myModel.BranchId }); 
} 

public class MyModel 
{ 
    public int BranchId { get; set; } 
} 

的观点:

<div> 
    @using (Html.BeginForm("BranchDetails", "Home", FormMethod.Post)) 
    { 
     @Html.TextBox("BranchId","123") 
     <input type="submit" value="Go"/> 
    } 
</div> 
-1

这会为你工作。干杯:D

return RedirectToAction(“Index”,“ControllerName”,new {branchId = branchId});

+0

最初试过这个,如果变量名和查询字符串一样,不需要,谢谢! – AlwaysLearning

1

@MichaelLake感谢您的文章我发现这个问题。我试过你的代码,果然它按预期工作。我没有提到我正在使用装有分支的Kendo组合框控件(!)。我没有提到,因为我需要的实际数据在post方法中是可用的,所以认为问题出在Controller方法上。我将Kendo的控制名称作为BranchList,将其更改为BranchId,现在可以按照预期的方式使用原始代码!剑道名称成为元素ID,并且必须匹配才能工作。

非常感谢!

相关问题