gpt4 book ai didi

asp.net - 将 RouteValues 读入 Controller

转载 作者:行者123 更新时间:2023-12-02 22:29:45 25 4
gpt4 key购买 nike

所以我有一个 Url Action

<a href="@Url.Action("Create","Teacher", new { createAndAssign = true, teacherID = Model.AccountID })">Create new teacher & assign to account.</a>

传入两个路由值:createAndAssign 和 teacherID。

现在当我转到我的教师/创建页面时,我的 URL 是这样的:

.../Teacher/Create?createAndAssign=True&teacherID=ea817321-5633-4fdc-b388-5dba2c4a728e

这很好,我想要这个。现在,当我通过 POST 创建我的老师时,如何获取 createAndAssign 和 teacherID 值?

最佳答案

您可以在表单的隐藏变量中设置查询字符串值并在 GET 操作方法中呈现并在 POST 操作方法中接受它。

View 由您的 GET 操作呈现

@using (Html.BeginForm())
{
//Other form elements also
@Html.Hidden("teacher",@Request.QueryString["teacherID"] as string)
@Html.Hidden("createAndAssign",@Request.QueryString["createAndAssign"]
as string)
<input type="submit" />
}

现在在您的 HttpPost 操作方法中有一个 teacher 参数和 createAndAssign 参数,以便在您提交表单时可以使用它。

[HttpPost]
public ActionResult Create(string teacher,string createAndAssign)
{
//Save and Redirect
}

如果你的 View 是强类型的(这是我个人的偏好),这很容易,

public ActionResult GET(string teacherID,string createdAndAssing)
{
var yourVMObject=new YourViewModel();
yourVMObject.TeacherID=teacherID;
yourVMObject.CreateAndAssign=createdAndAssing;
return View(createdAndAssing);
}

在你的强类型 View 中,

@model YourViewModel
@using (Html.BeginForm())
{
//Other form elements also
@Html.HiddenFor(x=>x.TeacherID)
@Html.HiddenFor(x=>x.CreateAndAssign)
<input type="submit" />
}

并且在您的POST 操作中

[HttpPost]
public ActionResult Create(YourViewModel model)
{
//look for model.TeacherID
//Save and Redirect
}

关于asp.net - 将 RouteValues 读入 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12497142/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com