从另一个动作或另一个视图调用动作

时间:2019-12-11 17:43:40

标签: html asp.net-mvc asp.net-core-mvc

我有一个名为DappAccount的控制器,其中有一个操作AccountMainPage,它接收类作为参数。在其中执行登录并创建返回的模型,然后直接进入AccountMainPage和Layout视图。 在布局中,我有一个链接(标签),我希望按下该链接将回调AccountMainPage操作,但是我不知道如何发回模型。 这是我尝试过的:

[HttpPost]
    public async Task<IActionResult> AccountMainPage(DappAccount account) //here the login succeeded 
    { 
        bool IsValidated = account.CheckLogin(account.publicKey, account.privateKey);  
        if (!IsValidated)
            return RedirectToAction("Login", "Home");

        account.ConnectToBlockchain();
        myAccount = account; //save this account in static controller property so the other controllers be able to read it
        myAccount.OwnAssetsList = await _context.Assets.FromSqlRaw("select * from Assets where OwnerPublicKey = {0}", account.publicKey).ToListAsync();
        return View(account);

    }

还有link元素,但是失败了,因为我认为因为它没有发回模型:

   <a id="assetsNavBar" class="nav-link" asp-area="" asp-controller="DappAccount" asp-action="AccountMainPage">Assets<span class="sr-only">(current)</span></a>

我也尝试过:

  <a id="assetsNavBar" class="nav-link" asp-area="" asp-controller="DappAccount" asp-action="ShowAssetPage">Assets<span class="sr-only">(current)</span></a>

我成功调用了ShowAssetPage(),但重定向也失败了:

 public async Task<IActionResult> ShowAssetPage()
    {
        return RedirectToAction("AccountMainPage", myAccount); //here the view didn`t load too
    }

如何成功调用AccountMainPage?

3 个答案:

答案 0 :(得分:2)

[HttpGet] //Thanks to @Xing Zou
[HttpPost]
    public async Task<IActionResult> AccountMainPage(DappAccount account) //here the login succeeded , e initialized the key
    { 
        bool IsValidated = account.CheckLogin(account.publicKey, account.privateKey); //so now call againg to the login method in order to load more properties 
        if (!IsValidated)
            return RedirectToAction("Login", "Home");

        account.ConnectToBlockchain();
        myAccount = account; //and save this account in static account so the other controllers be able to read it
        myAccount.OwnAssetsList = await _context.Assets.FromSqlRaw("select * from Assets where OwnerPublicKey = {0}", account.publicKey).ToListAsync();
        return View(account);

    }

在“布局”视图中:

     <a id="assetsNavBar" class="nav-link" asp-area="" asp-controller="DappAccount" asp-action="AccountMainPage" asp-route-account.publicKey="@Model.publicKey" asp-route-account.privateKey="@Model.privateKey">Assets<span class="sr-only">(current)</span></a>
 <!--Thanks to @user12376840-->

答案 1 :(得分:1)

您需要在AccountMainPage操作中将参数作为路由传递,因此请尝试类似的操作。

   <a id="assetsNavBar" class="nav-link" asp-area="" asp-controller="DappAccount" asp-action="AccountMainPage" asp-route-account="@new DappAccount(){}">Assets<span class="sr-only">(current)</span></a>

确保您在{}内添加字段或传递一个已经可以访问的DappAccount对象

答案 2 :(得分:1)

如果我理解正确,则您正在尝试使用<a>重定向到POST操作。但是,锚标记和重定向都发送GET请求。

您需要在GET控制器中添加DappAccount动作以返回AccountMainPage视图

[HttpGet]
public IActionResult AccountMainPage() 
{
   var account = ...;
   //get your account 
   return View(account)
}