【问题标题】:C# WebApplication POSTC# Web 应用程序 POST
【发布时间】:2016-07-21 15:04:39
【问题描述】:

我正在尝试使用 C# 发布表单

我进行了一些搜索,但我无法正确编码(我是这个领域的新手)。

这是我的代码;

查看;

<form>
          <div class="field-wrap">
              <label>
                  Email Address<span class="req">*</span>
              </label>
              <input type="email" id="input-username" name="Username" required autocomplete="on" />
          </div>

          <div class="field-wrap">
            <label>
              Password<span class="req">*</span>
            </label>
            <input type="password" id="input-password" name="Password" required autocomplete="on"/>
          </div>

          <p class="forgot"><a href="#">Forgot Password?</a></p>

          <button class="button button-block" id="button-login">Log In</button>

      </form>

控制器;

// GET: User
        [HttpPost]
        public ActionResult Login()
        {
            string username = Session["Username"].ToString();
            string password = Session["Password"].ToString();

            Service iLocationService = new Service();
            var result = Service.MemberGetLogin( username, password, "127.0.0.1" );

            ViewBag.Message = result;

            return View();
        }

Javascript;

jQuery(document).ready(function () {
$("#button-login").click(function () {
    $.ajax({
        type: "POST",
        url: "/Controllers/UserController/login/",
        data: $(this).serialize(),
        dataType: "json"
    })
    .done(function (result) {
        console.log(result);
    })
    .fail(function (a) {
        console.log( a);
    });
});

});

我要做的是发布输入值以检查用户。

提前致谢

【问题讨论】:

    标签: javascript c# asp.net-mvc asp.net-ajax


    【解决方案1】:

    看看这一行

    字符串用户名 = Session["用户名"].ToString();

    在您的代码中,您尝试从 Session 变量中读取用户名和密码值。谁将用户名和密码设置为 Session ?您应该从发布的表格中阅读这些内容并使用它。

    [HttpPost]
    public ActionResult Login(string userName,string password)
    {
      // do something with userName and password and return something
    }
    

    此外,您需要确保您正在序列化表单,而不是单击的按钮。我个人更喜欢使用 Html helper 方法来生成表单标签,并在我的 javascript 代码中使用表单的 action 属性值,而不是对 url 进行硬编码。

    所以在我看来

    @using(Html.BeginForm("login","User"))
    {
        //Your existing form inputs goes here
       <button class="button button-block" id="button-login">Log In</button>
    }
    

    在脚本中

    $("#button-login").click(function () {
         $.ajax({
            type: "POST",
            url: $(this).closest("form").attr("action"),
            data: $(this).closest("form").serialize()           
         })
    });
    

    由于您正在执行 ajax 表单提交,我建议您返回一个 json 响应,您的客户端代码可以解析该响应并做进一步的事情。

    [HttpPost]
    public ActionResult Login(string userName,string password)
    {
       //if userName and password are valid
           return Json(new { Status="success"});
       // else
             return Json(new { Status="failed", Message="Invalid credentials});
    }
    

    在你完成的回调中,你应该检查这个值并做进一步的事情

    .done(function (result) {
       if(result.Status==="success")
       {
         window.location.href="/Home/Index"; // change to wherever you want to redirect to
       }
       else
       {
         alert(result.Message);
       }    
    })
    

    【讨论】:

    • 您输入的用户名和密码的名称与您的参数名称不匹配。我还会给出一个明确的 url 并将按钮更改为输入 type=submit,这样你就有了一些一致的 html
    • 参数名称大小写不匹配。它仍然可以工作。此外,他正在做一个 ajax 表单发布(他在其中指定 url 变量。)因此无需在标记中指定表单操作值。但我更喜欢这样做,以便我可以读取最接近的表单操作值而不是硬编码。
    • 不是数据:$(this).serialize(),作用域是按钮而不是表单?我会明确地获取表单并序列化该元素。
    • 没错。我错过了那部分。现在更正了。感谢您的关注。
    • 您不需要在UserController 上指定文件夹名称(Controllers)和ControllerSuffix。在我使用 Html 帮助器呈现表单标签的地方查看我的更新答案
    猜你喜欢
    • 1970-01-01
    • 2013-06-16
    • 1970-01-01
    • 2011-07-06
    • 2015-11-11
    • 1970-01-01
    • 1970-01-01
    • 2015-01-08
    • 2010-11-27
    相关资源
    最近更新 更多