2

我有mvc應用程序和webApi應用程序。我需要通過webApi在asp.net mvc中登錄。當我使用這種方法:Asp.net mvc通過httpClient連接到webApi

public static string GetToken(LoginModelView login) 
     { 
      var pairs = new List<KeyValuePair<string, string>> 
         { 
          new KeyValuePair<string, string>("grant_type", "password"), 
          new KeyValuePair<string, string>("username", login.Login), 
          new KeyValuePair<string, string> ("Password", login.Password) 
         }; 
      var content = new FormUrlEncodedContent(pairs); 
      using (var client = new HttpClient()) 
      { 
       var response = 
        client.PostAsync(URL + "/Token", content).Result; 
       var responseJson = response.Content.ReadAsStringAsync().Result; 
       var dictionaryToken = JsonConvert.DeserializeObject<Dictionary<string,string>>(responseJson); 
       string token = dictionaryToken["access_token"]; 
       return token; 
      } 
     } 

我的mvc不綁定模型從視圖。 操作方法

[HttpGet] 
     public ActionResult SignIn() 
     { 
      var login = new LoginModelView(); 
      return View("SignIn",login); 
     } 

     [HttpPost] 
     public ActionResult SignIn(LoginModelView login) 
     { 
      string token = DirService.GetToken(login); 

      Session["token"] = token; 

      return RedirectToAction("success"); 
     } 

這是模型

public class LoginModelView 
    { 
     public string Login { get; set; } 

     public string Password { get; set; } 
    } 

這是我的看法

@model AdminTool.Models.LoginModelView 
    @using (Html.BeginForm()) 
    { 
     @Html.AntiForgeryToken() 

     <div class="form-horizontal"> 
      <h4>LoginModelView</h4> 
      <hr /> 
      @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 
      <div class="form-group"> 
       @Html.LabelFor(model => model.Login, htmlAttributes: new { @class = "control-label col-md-2" }) 
       <div class="col-md-10"> 
        @Html.EditorFor(model => model.Login, new { htmlAttributes = new { @class = "form-control" } }) 
        @Html.ValidationMessageFor(model => model.Login, "", new { @class = "text-danger" }) 
       </div> 
      </div> 

      <div class="form-group"> 
       @Html.LabelFor(model => model.Password, htmlAttributes: new { @class = "control-label col-md-2" }) 
       <div class="col-md-10"> 
        @Html.EditorFor(model => model.Password, new { htmlAttributes = new { @class = "form-control" } }) 
        @Html.ValidationMessageFor(model => model.Password, "", new { @class = "text-danger" }) 
       </div> 
      </div> 

      <div class="form-group"> 
       <div class="col-md-offset-2 col-md-10"> 
        <input type="submit" value="Login" class="btn btn-default" /> 
       </div> 
      </div> 
     </div> 
    } 

當我不使用該方法,一切都很好。 相同的代碼在另一個應用程序中起作用。 有人可以幫助我,請..

+0

你是什麼意思「不綁定模型」。被稱爲/被擊中的動作是? – Nkosi

+0

我的意思是我得到空,而不是填充對象 –

回答

0

我想你從數據發佈到獲取方法(GetToken)。

1

我注意到,在LoginModelView類有Login財產,你在controller.I創建同一個對象的名稱LoginModelView login改變代碼和它爲我工作。

型號: -

  [HttpGet] 
      public ActionResult SignIn() 
      { 
       var loginModelView = new LoginModelView(); 
       return View("SignIn", loginModelView); 
      } 

      [HttpPost] 
      public ActionResult SignIn(LoginModelView loginModelView) 
      { 
       string token = DirService.GetToken(loginModelView); 

       Session["token"] = token; 

       return RedirectToAction("success"); 
      } 

希望它的工作!

快樂編碼!

+0

謝謝噓多了!快樂編碼! –

+0

@ВладЛуценко您非常歡迎。 –