2

I have mvc application and webApi application. I need sign in in asp.net mvc through webApi. When i use this method:

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;
            }
        }

My mvc don't bind model from view. Action methods

[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");
        }

It's model

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

        public string Password { get; set; }
    }

It's my view

@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>
    }

when i don't use that method, everything is ok. The same code works in another application. Somebody can help me, please..

2
  • what do you mean by don't bind model. Is the action being called/hit? Commented Apr 15, 2017 at 13:38
  • I mean i get null instead filled object Commented Apr 15, 2017 at 16:31

2 Answers 2

2

I noticed that in LoginModelView class has Login Property and you create same object name LoginModelView login in controller.I change that code and its Work For Me.

Model:-

           [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");
            }

Hope Its Work !!!

Happy Coding !!

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you soo much !!!! Happy Coding !!!
0

I think you posting from data to get method(GetToken).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.