【问题标题】:ASP.NET MVC Redirect To Action does not render final ViewASP.NET MVC Redirect To Action 不呈现最终视图
【发布时间】:2016-03-23 16:18:52
【问题描述】:

我正在尝试这段代码:-

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

我可以在没有查询字符串的情况下成功运行代码。 我什至浏览了索引视图并且可以看到模型正在工作,但是索引视图没有呈现,分支选择器视图仍然存在。网络开发人员工具在执行重定向时显示正确的 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 });
}

【问题讨论】:

  • 你能分享你的索引视图代码吗??
  • 索引代码只是将查询字符串解析为 INT,然后使用返回模型的服务。谢谢!
  • AlwaysLearning:这与经典 ASP 无关,请重新标记您的问题。

标签: asp.net asp.net-mvc razor routing


【解决方案1】:

尝试在帖子中使用强类型模型,并将参数指定为实际参数 - 使用视图模型对你来说会更好。

我已经测试了以下 - 它似乎对我来说按预期工作:

[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>

【讨论】:

    【解决方案2】:

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

    非常感谢!

    【讨论】:

      【解决方案3】:

      这对你有用。干杯:D

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

      【讨论】:

      • 最初尝试过,如果变量名与查询字符串相同,则不需要,谢谢!
      猜你喜欢
      • 2015-03-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多