将值传递给asp.net中的路由mvc5

时间:2017-10-02 14:30:54

标签: c# asp.net .net asp.net-mvc

我的第一个控制员:

public ActionResult Create()
{
    return View();
}

[HttpPost]
public ActionResult Create(personalInfoModel personalInfo)
{
    if (ModelState.IsValid)
    {
        TempData["key"] = personalInfo.CNIC;

        PersonalInfoViewModel pivm = new PersonalInfoViewModel();
        pivm.AddNewRecord(personalInfo);

        return RedirectToAction("Create", "Experience", new { key = personalInfo.CNIC});
    }

    return View();
}

我的第二个控制器代码是:

public ActionResult Create(string key)
{
    if (filled == true)
    {
        TempData["alertMessage"] = "<script>alert('Fill it first!')</script>";
    }
    filled = true;
    return View(key);
}

[HttpPost]
public ActionResult Create(experiencesModel experiences, string key)
{
    filled = false;
    ExperiencesViewModel evm = new ExperiencesViewModel();
    evm.AddNewRecord(experiences, key);
    return View();

}

我想将密钥从第一个控制器传递到我遇到错误的第二个控制器:

  

视图&#39; 42201-09007860-1&#39;或者找不到它的主人,或者没有视图引擎支持搜索到的位置。搜索了以下位置:   〜/查看/经验/ 42201-09007860-1.aspx   〜/查看/经验/ 42201-09007860-1.ascx   〜/查看/共享/ 42201-09007860-1.aspx   〜/查看/共享/ 42201-09007860-1.ascx   〜/查看/经验/ 42201-09007860-1.cshtml   〜/查看/经验/ 42201-09007860-1.vbhtml   〜/查看/共享/ 42201-09007860-1.cshtml   〜/查看/共享/ 42201-09007860-1.vbhtml

我该如何解决这个问题?我需要澄清从控制器到控制器的值传递。

2 个答案:

答案 0 :(得分:1)

View的第一个参数是要渲染的视图的名称。如果要根据约定使用默认视图,则必须传递null。第二个参数是您可以传递模型的地方,该模型可能是您的key

return View(null, key);

或者

return View("Create", key);

答案 1 :(得分:1)

我认为您不完全理解return View()代码。这是View()View("ViewName")

之间的区别
  

return View()返回与action方法对应的视图   return View("ViewName")返回当前视图文件夹中指定的视图名称。

在这一行:

return View(key);

您正在尝试返回具有key参数中提供的名称的视图,该参数当然不会起作用。如果您想查看Create页面,只需将其更改为以下行:

return View("Create", key);