[英]How do you pass data from one view to another using the same controller but different actions? ASP.Net & Microsoft Graph
Okay, so the idea is that the user clicks on a dropdown selection on the view, hits Submit on the form, which sends the user choice back to the Controller, and renders a new view specific to that user choice.好的,所以这个想法是用户点击视图上的下拉选择,点击表单上的提交,将用户选择发送回控制器,并呈现特定于该用户选择的新视图。
This is the view:这是视图:
<form method="post">
<label for="teamList">First choose a team:</label>
<select id="teamList" name="ID">
@foreach (var item in Model)
{
<option value=@item.Id>@item.DisplayName</option>
}
</select><br />
<input type="submit" value="Get channels">
</form>
The post method on the form would take the value of the user option and pass it to the Controller with action 'Index':表单上的 post 方法将获取用户选项的值并将其传递给具有操作“索引”的控制器:
[HttpPost]
public async Task<ActionResult> Index(string ID)
{
var data = await GraphHelper.GetChannelsAsync(ID);
return View("Channels", data);
}
Now it does render a 'Channels' view with the below html:现在它确实使用以下 html 呈现“频道”视图:
<form method="post">
<label for="channelList">Now choose a channel:</label>
<select id="channelList" name="channelId">
@foreach (var item in Model)
{
<option value=@item.Id>@item.DisplayName</option>
}
</select><br />
<input type="submit" value="Create tabs">
</form>
Now, here is where I need some help.现在,这是我需要帮助的地方。 I need to submit this channelList form again to create a tab using the Microsoft Graph API.我需要再次提交这个 channelList 表单以使用 Microsoft Graph API 创建一个选项卡。 I would submit this 'channelList' form back to my controller with the 'channelId' value.我会将此“channelList”表单提交回带有“channelId”值的控制器。
public async Task<ActionResult> SubmitChannelId()
{
//Take channelID and pass it to microsoft.graphApi function call here
return View(//with returned function call response);
}
How can I do a [HttpPost] submit request on this 'Channels' view if I'm using the same controller?如果我使用相同的控制器,如何在此“频道”视图上执行 [HttpPost] 提交请求? This probably has something to do with actions, or actionLinks but I've read that documentation and couldn't make sense of it.这可能与操作或 actionLinks 有关,但我已经阅读了该文档并且无法理解它。 Can I get some assistance?我可以得到一些帮助吗? Thanks!谢谢!
Quick solution would be to just use the Url helper on form element:快速解决方案是只在表单元素上使用 Url 助手:
<form method="post" action="@Url.Action("MyAction", "MyController")" >
You could as well use Html.BeginForm
to render whole form:您也可以使用Html.BeginForm
来呈现整个表单:
@using(Html.BeginForm("MyAction", "MyController", FormMethod.Post))
{
< ... >
}
In both cases your action SubmitChannelId
in Controller should be decorated with [HttpPost]
and accept input param of submited channelId
(change type of param accordingly):在这两种情况下,您在 Controller 中的操作SubmitChannelId
应该用[HttpPost]
修饰并接受提交的channelId
输入参数(相应地更改参数类型):
[HttpPost]
public async Task<ActionResult> SubmitChannelId(int channelId)
{
< ... >
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.