【问题标题】:passing value to route in asp.net mvc5将值传递给asp.net mvc5中的路由
【发布时间】:2017-10-02 14:30:53
【问题描述】:

我的第一个控制器:

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();

}

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

未找到视图“42201-09007860-1”或其主视图,或者没有视图引擎支持搜索到的位置。搜索了以下位置: ~/Views/Experience/42201-09007860-1.aspx ~/Views/Experience/42201-09007860-1.ascx ~/Views/Shared/42201-09007860-1.aspx ~/Views/Shared/42201-09007860-1.ascx ~/Views/Experience/42201-09007860-1.cshtml ~/Views/Experience/42201-09007860-1.vbhtml ~/Views/Shared/42201-09007860-1.cshtml ~/Views/Shared/42201-09007860-1.vbhtml

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

【问题讨论】:

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


【解决方案1】:

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

return View(null, key);

或者

return View("Create", key);

【讨论】:

    【解决方案2】:

    我认为您并不完全理解return View() 代码。这是View()View("ViewName")之间的区别:

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

    在这一行:

    return View(key);
    

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

    return View("Create", key);
    

    【讨论】:

      猜你喜欢
      • 2017-08-04
      • 2011-03-13
      • 2015-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多