问题
I am using asp.net mvc
And I have a login model with three fields: -baliecode -username -password
So every balie code correspondents with a username and password.
And now I try to get the username and password in the texfields if a user has entered a baliecode an press TAB.
The action method looks likes this:
[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);
}
The models looks like this:
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; }
}
and this is the view with ajax call:
@{
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>
}
来源:https://stackoverflow.com/questions/43343272/how-to-return-data-from-model-in-textfields-with-ajax-call