iT邦幫忙

1

如何在ASP.NET MVC專案中 判別會員成功登入跳出想要的資料 而不是開啟一個新的網頁

  • 分享至 

  • xImage

如何在ASP.NET MVC專案中,判別會員成功登入跳出想要的資料,而不是開啟一個新的網頁

敘述若有不清楚的地方可以再提出

是要用什麼方法才正確,有參考資料提供嗎?

先謝謝各位了

補充:就是類似帳號登入後,可以自動出現他的學生課表,而不是還要寫一個超連結讓使用者去 點連結導過去,希望登入後可以直接顯示下面第三張圖的資料,資料是要在第二張顯示

https://ithelp.ithome.com.tw/upload/images/20210813/20140491mvYB5xMo4C.png

https://ithelp.ithome.com.tw/upload/images/20210813/201404910uQ8F34dHc.png

https://ithelp.ithome.com.tw/upload/images/20210813/20140491bPF7js5jgw.png

先提示你幾個關鍵字。
最主要的是「AJAX」
再來可以利用的框架有JQUERY、VUE...

總之這是前端的技術。
至於.NET是否有可以做到這一點。
因為這我不熟沒辦法跟你說明。就留待會的人跟你說了。
MVC再套jquery或vue,有點像是在火車上開汽車.....個人觀點,MVC也有自己的postback跟獨立的ajax,只是用起來沒有js框架那麼簡單就是了
Yaowen iT邦研究生 4 級 ‧ 2021-08-13 09:55:56 檢舉
AJAX or JQUERY 利用這2個功能去加 view的程式...
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
0
小魚
iT邦大師 1 級 ‧ 2021-08-13 09:40:24

.Net MVC做Ajax是可以的.

1
Hankz
iT邦新手 2 級 ‧ 2021-08-13 10:07:19

使用表單送出的方式進行驗證,可以在後端寫:

return RedirectToAction(actionName, controllerName);`

若是使用Ajax(jQuery):

$.post(
    apiUrl,
    postData,
    function(result) {
        if(result.isSuccess) {
            document.location.href = targetURL;
        } else {
            // other...
        }
    },
    "json"
);
2
海綿寶寶
iT邦大神 1 級 ‧ 2021-08-13 10:12:44

看得出來你已經寫了第二張圖和第三張圖的程式(以下簡稱 p2,p3)

有兩個方法:
1.把登入成功之後 跳轉/顯示到 p2 改成 p3
2.把 p3 的程式貼到 p2

1
froce
iT邦大師 1 級 ‧ 2021-08-13 10:32:08

補充:就是類似帳號登入後,可以自動出現他的學生課表,而不是還要寫一個超連結讓使用者去 點連結導過去,希望登入後可以直接顯示下面第三張圖的資料,資料是要在第二張顯示

有個東西叫 redirect
https://jeffprogrammer.wordpress.com/2016/05/18/asp-net-mvc-redirect-to-view/

0
japhenchen
iT邦超人 1 級 ‧ 2021-08-13 10:58:40

還有一個更簡單的方法,在前端HTML裡加個
<% Response.WriteFile("userinfo.aspx"); %>
加在哪裡你自己看著辦,這是我的網站上的截取的

不用改什麼ajax也不用什麼redirect也不會有任何跳轉或postback更不用vue或jquery,單MVC就可以放心服用

<%@ Page Language="C#" AutoEventWireup="true" Debug="true" CodeFile="Default.aspx.cs" Inherits="costctrl_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="X-UA-Compatible" content="IE=edge;chrome=1" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>網頁網頁網頁</title>
    <link href="main.css" rel="stylesheet" />
</head>
<body>
    <!-- .....這是你的網頁前端內容,我省略10001個字因為我看不見.... //--->
    
    
    <table>
        <tr>
            <th>個人資料</th>
        </tr>
        <tr>
            <td>
                <% Response.WriteFile("userinfo.aspx"); %>
            </td>
        </tr>
    </table>
</body>
</html>

如果你的慣用語言是VB或其他,請自己改一下語法,這是C#的

我忘了把X-UA-Compatatible拿掉了,那行有沒有都無所謂

用這招的話,被writefile進來的頁面內容盡可能不要加<html><head><body>,因為不是所有的瀏覽器看的懂HTML嵌套

至於上述例子你不一定要用table來當容器,一切以你的版面配置為主,或許你做的是Bootstrap RWD,愛怎麼裝都不是問題啦

PS如果被加掛進來的頁面沒有自己的head,那內容style跟script會繼承主頁的哦

0
Yaowen
iT邦研究生 4 級 ‧ 2021-08-13 11:52:37
@using (Html.BeginForm("Index", "Login", FormMethod.Post, new { enctype = "multipart/form-data" }))
    {
}

BeginForm 只需要改成 Index(ActionName) Login(ControllerName)

假設P3 是 ClassController 的 Index2(ActionName)

只要在 Login View BeginForm 改成 Html.BeginForm("Index2", "Class") 即可

如果是同一個Controller Index指需要改成 Class
然後到Controller 的Action 後 自己再決定View路徑...

   return RedirectToAction("Index2", "Class");

下面是我自己範例 Login 轉到 Home
LoginView部分

@{
    /**/
    ViewBag.Title = "XXX使用者登入";
}

@model  WebApplication1.Models.Login.Login


<br>
<h3>XXXX管理系統</h3>

@if (ViewBag.ResultMessage != null)//判斷如果有訊息,則顯示。
{
    //@Html.Label("info", (string)ViewBag.ResultMessage, new { @class = "text-info" })
    <script type="text/javascript">
            var message = @Html.Raw(Json.Encode(ViewBag.ResultMessage));
            alert(message);
    </script>
}

@using (Html.BeginForm("Index", "Login", FormMethod.Post, new { enctype = "multipart/form-data" }))
    {
        @Html.AntiForgeryToken()
        <div class="container">
            <hr />
            <div class="" style="padding-top: 10px">
                <div class="column">
                    @Html.LabelFor(model => model.UserID, htmlAttributes: new { @class = "control-label col-md-2" })
                    <div class="col-md-10">
                        @Html.EditorFor(model => model.UserID, new { htmlAttributes = new { @class = "form-control" } })
                        @Html.ValidationMessageFor(model => model.UserID, "", new { @class = "text-danger" })
                    </div>
                </div>
            </div>
            <br></br>
            <div class="" style="padding-top: 10px">
                <div class="column">
                    @Html.LabelFor(model => model.UserPwd, htmlAttributes: new { @class = "control-label col-md-2" })
                    <div class="col-md-10">
                        @Html.EditorFor(model => model.UserPwd, new { htmlAttributes = new { @class = "form-control" } })
                        @Html.ValidationMessageFor(model => model.UserPwd, "", new { @class = "text-danger" })
                    </div>
                </div>
            </div>
            <div class="" style="padding-left: 40px;padding-top: 10px">
                @Html.ValidationMessage("password", null, new { @class = "text-danger", style = "margin-left:10px;" })
            </div>
            <form>
                <div style="padding-right: 70px; padding-top: 10px">
                    <input id="Submit" type="submit" value="確定" style="padding: 5px 20px;margin-left:10px;">
                </div>
            </form>

        </div>
    }

LoginController部分

[HttpPost]
[ValidateAntiForgeryToken]
[AllowAnonymous]
public async Task<ActionResult> Index(Login postback){
try{
   if (this.ModelState.IsValid){
   var receivePassword = Login.Encoded(postback.UserPwd +  postback.UserID);
   if (await Login.LoginAccountAsync(postback.UserID, receivePassword))
   {
      Session["UserID"] = postback.UserID;
      return RedirectToAction("Index", "Home");
   }
   else{
     ModelState.AddModelError("password", "登入失敗,請確認登入資訊。");
     return View("Index");
   }
   }
   else {
             return View("Index");
   }
  }
 catch (Exception e){
       ViewBag.ResultMessage = e.ToString();
       return View();
  }
  }

HomeController 部分

      public ActionResult Index()
        {
            return View();
        }
Homura iT邦高手 1 級 ‧ 2021-08-13 14:17:31 檢舉

覺得這個超難用...

1
天黑
iT邦研究生 5 級 ‧ 2021-08-13 17:24:11

我理解的是你需要登入後,跳轉到你需要的頁面,而不是首頁

return RedirectToAction("Index", "Home");

所以調整登入後的程式跳轉到你需要的頁面就可以了,
Index代表的是action,Home代表的是controller,一般就是預設的首頁,
因為你提供的畫面沒有連結,我不確定你需不需要帶參數,
不過是抓使用者資訊,理論上不需要,可以直接重登入資訊取

我要發表回答

立即登入回答