gpt4 book ai didi

c# - .Net MVC 中带有 AJAX 的嵌套表单

转载 作者:行者123 更新时间:2023-11-30 21:15:13 29 4
gpt4 key购买 nike

您好,我有一个表单,需要使用 AJAX 检查用户名字段的可用性。这是我的代码..

 <% using (Html.BeginForm()) { %>
<%: Html.ValidationSummary(true, "Account creation was unsuccessful. Please correct the errors and try again.") %>
<div>
<fieldset>
<legend>Account Information</legend>

<div class="editor-label">
<%: Html.LabelFor(m => m.UserName) %>
</div>
<% using (Ajax.BeginForm("Checkavailability", new AjaxOptions { UpdateTargetId = "textEntered" }))
{ %>
<div class="editor-field">
<%: Html.TextBoxFor(m => m.UserName) %>
<%: Html.ValidationMessageFor(m => m.UserName) %>
</div>
<input type="submit" value="Check Avail" id="Submitt"/><br />
<span id="textEntered">Nothing Entered</span>
<% } %>


<div class="editor-label">
<%: Html.LabelFor(m => m.Email) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(m => m.Email) %>
<%: Html.ValidationMessageFor(m => m.Email) %>
</div>

<div class="editor-label">
<%: Html.LabelFor(m => m.Password) %>
</div>
<div class="editor-field">
<%: Html.PasswordFor(m => m.Password) %>
<%: Html.ValidationMessageFor(m => m.Password) %>
</div>

<div class="editor-label">
<%: Html.LabelFor(m => m.ConfirmPassword) %>
</div>
<div class="editor-field">
<%: Html.PasswordFor(m => m.ConfirmPassword )%>
<%: Html.ValidationMessageFor(m => m.ConfirmPassword) %>
</div>

<p>
<input type="submit" value="Register" />
</p>
</fieldset>
</div>
<% } %>

问题是当我点击 check avil 按钮时出现验证。有没有办法只提交 ajax 表单并更新而不提交主表单?或任何方式来实现我的检查可用性逻辑?

最佳答案

我删除了 AJAX.BeginForm 并添加了 Ajax.ActionLink 来提交表单..相反,它将单个参数传递给我需要检查的 Controller ..这是代码..

 <% using (Html.BeginForm()) { %>
<%: Html.ValidationSummary(true, "Account creation was unsuccessful. Please correct the errors and try again.") %>

<fieldset>
<div class="editor-label">
<%: Html.LabelFor(m => m.UserName) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(m => m.UserName) %>
<%: Html.ValidationMessageFor(m => m.UserName) %>
</div>
<%= Ajax.ActionLink("Checkavailability", "Checkavailability", new { UserName = "" }, new AjaxOptions { UpdateTargetId = "textEntered" }, new { id = "u" })%>



<span id="textEntered">Nothing Entered</span>



<p>
<input type="submit" value="Register" />
</p>


</fieldset>

<% } %>

下面是用于设置传递参数的jQuery

  <script type="text/javascript">

$(document).ready(function () {
$('#UserName').blur(function () {
changeActionURL();
});

});



function changeActionURL() {

if ($("#UserName").val() == "") {
alert('Please enter your email to check availablity');
}
else {


var url = '<%= new UrlHelper(ViewContext.RequestContext).Action("Checkavailability", "Account") %>' + '?UserName=' + $("#UserName").val();

$("#u").attr('href', url);
}
}




</script>

Controller 是..

public string Checkavailability(string UserName)
{
if (UserName != "Enter text" && !String.IsNullOrEmpty(UserName))
{
string userName = UserName.ToLower();
NorthwindEntities dbContext = new NorthwindEntities();
var query = from p in dbContext.Employees
where p.FirstName.ToLower() == userName
select p;
IEnumerable<Employee> rec = query.ToList();

if (rec.Count() == 0)
{
return "You entered: \"" + UserName.ToString() + "\" available ";
}
else
{
return "You entered: \"" + UserName.ToString() + "\" already exists " +
DateTime.Now.ToLongTimeString();
}

}

return String.Empty;
}

关于c# - .Net MVC 中带有 AJAX 的嵌套表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5809983/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com