如何解决使用Dropdownlist Visual Basic调用MVC actionResult
我希望下拉列表在onChange事件之后触发对ActionResult的调用,而不是将整个表单提交给服务器。想法是,必须先将所选项目发送到服务器,以检查所选公司在数据库中是否可用,然后才能继续填写其他详细信息。同样,当用户键入他们的姓名时,onChange还将触发一个事件以检查该用户是否在数据库中可用。
index.vbhtml
@Using Html.BeginForm("Authorise","Login",FormMethod.Post)
<div>
@Html.DropDownListFor(Function(x) x.SelectedItems,Model.CompanyID,New With {Key .id = "CompanyID"})
</div>
<div>
@Html.TextBoxFor(Function(model) model.UserName,New With {Key .id = "UserName"})
</div>
<div>
@Html.TextBoxFor(Function(model) model.UserPassword,New With {Key .id = "UserPassword"})
</div>
End Using
LoginController:
Function onChangeCompanyName() As ActionResult
.... code to check availability of selected value...
return View()
End Function
Function onChangeUsername() As ActionResult
.... code to check availability of selected value...
return View()
End Function
LoginModel.cs
Public Class LoginModel
Public Property CompanyID As String
Public Property LoginErrorMessage As String
Public Property Username() As String
Public Property UserPassword() As String
Public Property SelectedItems As IEnumerable(Of String)
Public Property Items As IEnumerable(Of SelectListItem)
End Class
解决方法
如果您没有安全问题,可以使用此 解决方案,按照您的要求进行操作,步骤如下:
- 在index.vbhtml中使用JQuery 从您的客户下拉列表中获取价值 然后使用AJAX通过POST发送数据
<script>
function checkMyId(id) {
$.ajax({
type: "POST",url: "/Home/AjaxMethodeCheck",data: '{id: "' + id + '" }',contentType: "application/json; charset=utf-8",dataType: "json",success: function (response) {
alert(response.message);
var value = response.value;
// do the job ...
},failure: function (response) {
alert("failure");
},error: function (response) {
alert("error");
},});
}
$(document).ready(function () {
$(function () {
$("#YourDropDownId").change(function () {
var selected = $(this).find("option:selected");
checkMyId(selected.val());
});
});
});
</script>
- 在您的控制器中,您将收到数据并进行检查
- 然后将结果以json格式发送回JQuery:
<HttpPost>
Public Function AjaxMethodeCheck(ByVal id As String) As JsonResult
Dim Str As String = "Done !" ' if you want to send back message ...
Dim value As Integer = 0 ' if you want to send back data ...
' Do your job in vb.net
Dim response = New With {
.id = id,.message = Str,.value = value}
Return Json(response)
End Function
- 使用该结果的jQuery(ajax成功:)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。