如何使用Ajax调用从文本字段中的模型返回数据?

时间:2017-04-11 10:23:54

标签: asp.net-mvc asp.net-ajax controllers

我正在使用asp.net mvc

我有一个包含三个字段的登录模型: -baliecode -用户名 -password

所以每个balie代码都有一个用户名和密码。

现在我尝试在texfields中输入用户名和密码,如果用户输入了baliecode并按TAB键。

动作方法看起来像这样:

[AcceptVerbs(HttpVerbs.Post)]
    public JsonResult GetLogindetails(V_LoginModel_BalieUser model)
    {

        ISalesPersonProfile salesAgent99 = CommerceFrameworkBase.SalesPersons.GetSalesPerson("HD");
        if (model.BalieCode == salesAgent99.Id)
        {
            model.UserName = salesAgent99.Email;
        }
        return Json(model);
    }

模型看起来像这样:

 public class V_LoginModel_BalieUser : LoginModel
        {

            public string BalieCode { get; set; }
    }

 //
    // Summary:
    //     A model to login into the webshop.
public class LoginModel
{
    public LoginModel();

    //
    // Summary:
    //     Gets or sets the password.
    [AllowHtml]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    [Required(ErrorMessageResourceName = "Validation_RequiredField")]
    [StringLength(30, ErrorMessageResourceName = "Validation_MaxLengthExceeded")]
    public virtual System.String Password { get; set; }
    //
    // Summary:
    //     Gets or sets a value indicating whether to remember the user to login him automatically
    //     on the next visit.
    [Display(Name = "Login_RememberMe")]
    public virtual System.Boolean RememberMe { get; set; }
    //
    // Summary:
    //     Gets or sets the username.
    [DataType(DataType.EmailAddress, ErrorMessageResourceName = "Validation_InvalidField")]
    [Display(Name = "EmailAddress")]
    [Required(ErrorMessageResourceName = "Validation_RequiredField")]
    [StringLength(80, ErrorMessageResourceName = "Validation_MaxLengthExceeded")]
    [TrimAttribute(new[] { })]
    public virtual System.String UserName { get; set; }
}

这是带有ajax调用的视图:

@{

    Layout = LayoutPaths.General;
}

@model Sana.Commerce.DomainModel.Account.V_LoginModel_BalieUser



<h2>Index</h2>
@Html.Label("Enter Your name")
@Html.TextBox("PassId")



<div class="semicolumn">
    <div class="form-holder">
        @using (Html.BeginForm(htmlAttributes: new { @class = "form" }))
        {
            @Html.AntiForgeryToken()




            <table>
                <tr>
                    <th>
                        <div id="balieCode">
                            @Html.DisplayNameFor(modelItem => modelItem.BalieCode)
                        </div>
                    </th>
                    <th></th>
                </tr>

                <tr>
                    <td>
                        @Html.TextBoxFor(modelItem => modelItem.BalieCode)
                    </td>

                </tr>



                <tr>
                    <th>
                        @Html.DisplayNameFor(modelItem => modelItem.UserName)
                    </th>
                    <th></th>
                </tr>

                <tr>
                    <td>
                        @Html.TextBoxFor(modelItem => modelItem.UserName)
                    </td>

                </tr>


                <tr>
                    <th>
                        @Html.DisplayNameFor(modelItem => modelItem.Password)
                    </th>
                    <th></th>
                </tr>

                <tr>
                    <td>
                        @Html.TextBoxFor(modelItem => modelItem.Password)
                    </td>

                </tr>


            </table>
            <div class="form-row">
                <h4></h4>
                <input type="submit" value="Login" />
            </div>
        }
    </div>
    <div>

    </div>
</div>

@section Scripts{
    @*<script src="~/Scripts/jquery-1.10.2.min.js"></script>
    <script src="~/Scripts/jquery-ui.min.js"></script>*@
    <script>


        $(document).ready(function () {

            $("#balieCode").change(function () {
                $.ajax({
                    type: "Post",
                    url: '@Url.Action("GetLogindetails", "profile")',
                    data: { id: $("").val() },
                    dataType: "json",
                    success: function (data) {
                        $("#UserName").val(data[0]);
                        $("#Password").val(data[1]);
                    }
                });
            })


        });
    </script>     

    }

0 个答案:

没有答案