简体   繁体   English

如何使用相同的控制器但不同的动作将数据从一个视图传递到另一个视图? ASP.Net 和 Microsoft Graph

[英]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.

相关问题 如何使用外键关系将数据从视图返回到 asp.net 中的 controller? - How do you return data from a view to controller in asp.net using foreign key relationship? 如何将数据从数据库从一个控制器传递到在ASP.NET Core中拥有自己的数据库的另一个控制器? - How do I pass data from a database from one controller to another controller that has its own database in ASP.NET Core? asp.net:如何在Controller和View之间传递相同的数据? - asp.net:How to pass the same data between Controller and View? ASP.NET MVC-如何将参数传递给其他控制器上的操作 - ASP.NET MVC - how to pass parameters to actions on a different controller 如何将数据从控制器传递到 ASP.NET MVC 中查看 - How to to pass data from controller to view in ASP.NET MVC 如何将模型从用于模型的编辑器传递到控制器? (使用asp.net) - How do you pass a model from Editor for model into a controller? (using asp.net) 将变量从视图传递到 controller 操作方法,然后传递到同一 ASP.NET MVC controller 中的另一个方法 - Pass a variable from a view to a controller action method and then to another method in the same ASP.NET MVC controller 如何使用 ASP.NET MVC 将数据从视图文本框传递到控制器 - How to pass data from the View textbox to Controller using ASP.NET MVC ASP.Net Mvc 如何使用 Ajax 使用按钮将数据从视图传递到 Controller - ASP.Net Mvc How to pass data from View into Controller with a Button using Ajax 当两个部分视图都包含在 ASP.NET MVC 中的同一父视图中时,如何将数据列表从一个部分视图传递到另一个部分视图? - How to pass a list of data from one partial view to another partial view, when both partial views are contained in a same parent view in ASP.NET MVC?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM