在VS2008中使用MVC项目模板(开箱即用)我注意到以下内容:
以下是Register.aspx表单的指定方式.
<% using (Html.BeginForm()) { %>
Run Code Online (Sandbox Code Playgroud)选择"注册"按钮而不提供任何帐户信息会显示此信息 帐户创建失败.请更正错误,然后重试.
•您必须指定用户名.
•您必须指定电子邮件地址.
•您必须指定6个或更多字符的密码.
我将Register.aspx表单更改为此.
<% using (Ajax.BeginForm("Register", new AjaxOptions { HttpMethod = "Post" })) { %>
Run Code Online (Sandbox Code Playgroud)选择"注册"按钮而不提供帐户信息显示没有错误.
问题:如何在使用Ajax.BeginForm时显示错误文本?
在ASP.Net MVC 3.0中,我使用的是Ajax.Beginform
并且在我调用jQuery函数的表单成功时命中JsonResult.但由于某种原因,我的表单重定向到JsonAction
我的看法
@using (Ajax.BeginForm("ActionName", "Controller", null, new AjaxOptions
{
HttpMethod = "POST",
OnSuccess = "ShowResult"
}, new { id = "myform" }))
{
// All form Fields
<input type="submit" value="Continue" class="button standard" />
}
Run Code Online (Sandbox Code Playgroud)
我的控制器
public JsonResult ActionName(FormCollection collection)
{
return Json(new { _status },JsonRequestBehavior.AllowGet);
}
Run Code Online (Sandbox Code Playgroud)
jQuery的
<script type="text/javascript">
function ShowResult(data) {
// alert("I am at ShowResult");
if (data.isRedirect) {
window.location.href = json.redirectUrl;
}
}
Run Code Online (Sandbox Code Playgroud)
出于某种原因,当我点击提交.它运行JSonResult并将页面重定向到我已包含的主机/控制器/动作名称
<script src="@Url.Content("jquery.unobtrusive-ajax.min.js")"></script>
Run Code Online (Sandbox Code Playgroud)
在我的layout.cshtml中
任何人都可以告诉我什么可能是错的?
我发现了这个问题.现在我必须找到提交的解决方案我正在验证我的表格
$("#myform").validate({
submitHandler: function (form) {
// …Run Code Online (Sandbox Code Playgroud) 我正在使用这个局部视图
@model CreateConfigEntityModel
<div class="row">
@using (Ajax.BeginForm("AddElement", "MerchantSites", new { merchantId = @Model.MerchantId }, new AjaxOptions
{
HttpMethod = "POST",
OnSuccess = "alert('ok')"
},
new { id = "addConfigForm" }
))
{
@Html.LabelFor(m => m.EntityName)
@Html.TextBoxFor(m => m.EntityName)
@Html.ValidationMessageFor(m => m.EntityName)
@Html.LabelFor(m => m.DefaultValue)
@Html.TextBoxFor(m => m.DefaultValue)
@Html.ValidationMessageFor(m => m.DefaultValue)
<input type="submit" value="Ajouter" class="tiny button" />
}
</div>
Run Code Online (Sandbox Code Playgroud)
调节器
public JsonResult AddElement(CreateConfigEntityModel model)
{
if (ModelState.IsValid)
{
_merchantSitesManager.AddEntity(model.EntityName, model.DefaultValue);
return Json(new { code = 1 }, JsonRequestBehavior.AllowGet);
}
else
return …Run Code Online (Sandbox Code Playgroud) 我有以下视图,其中包含一个Ajax.BeginForm: -
@using (Ajax.BeginForm("ChangeDevicesSwitch", "Switch", new AjaxOptions
{
InsertionMode = InsertionMode.InsertBefore,
UpdateTargetId = "result",
LoadingElementId = "progress2",
HttpMethod= "POST"
,
OnSuccess = "createsuccess",
OnFailure = "createfail"
}))
//code goes here
<p><img src="~/Content/Ajax-loader-bar.gif" class="loadingimage" id="progress2" /></p>
<div id ="result"></div>
Run Code Online (Sandbox Code Playgroud)
以及将从Ajax.Bginform调用的以下Action方法: -
public ActionResult ChangeDevicesSwitch(SwitchJoin s)
{//code goes here
try
{
var count = repository.changeDeviceSwitch(s.Switch.SwitchID, (Int32)s.GeneralSwitchTo, User.Identity.Name.Substring(User.Identity.Name.IndexOf("\\") + 1));
repository.Save();
return RedirectToAction("Details", new { id = s.GeneralSwitchTo });
}
catch (Exception e)
{
return Json(new { IsSuccess = "custome", description = "Error …Run Code Online (Sandbox Code Playgroud) 在局部视图中,我使用MVC Ajax.Beginform,如下所示:
<div id="divToReplace">
@using (Ajax.BeginForm("Action", "Controller,
new AjaxOptions
{
InsertionMode = System.Web.Mvc.Ajax.InsertionMode.Replace,
UpdateTargetId = "divToReplace"
},
new
{
id = "formID"
}))
{
...
</div>
Run Code Online (Sandbox Code Playgroud)
提交表单时,我希望孔div"divToReplace"被答案替换(部分视图再次).但相反,div"divToReplace"的内部html被答案所取代,因此部分视图的开头如下所示:
<div id="divToReplace">
<div id="divToReplace">
...
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
码:
<% using (Ajax.BeginForm("GetResourcesByProject", "CreateRequest", new AjaxOptions { UpdateTargetId = "ResourceListDiv"}))
{
Response.Write(Html.DropDownList("SelectProject", Model.ProjectList, "Select Project", new { onchange = "this.form.submit();" }));
} %>
Run Code Online (Sandbox Code Playgroud)
当我运行页面时,我得到正确的控制器操作,以使用表单集合中的正确数据进行触发:
public ActionResult GetResourcesByProject(FormCollection formCollection)
{
var resourceModels = (from project in POTSModel.ProjectList
where project.Id == Convert.ToInt32(formCollection["SelectProject"])
select project).First().Resources;
return PartialView("ResourceList", resourceModels);
}
Run Code Online (Sandbox Code Playgroud)
它从Ajax.ActionLink可以正常工作,如下所示:
<%= Ajax.ActionLink("Select", "GetResourcesByProject", "CreateRequest", new { projectId = item.Id }, new AjaxOptions { UpdateTargetId = "ResourceListDiv" })%>
Run Code Online (Sandbox Code Playgroud)
当帖子发生时,我将导航到新页面,而不是停留在现有页面上并更新div的内容.
谢谢.
我无法获得触发OnSuccess()方法的元素的目标Ajax.BeginForm().所以这是代码片段:
@using (Ajax.BeginForm("InsertModel", "Home", new AjaxOptions
{
HttpMethod = "POST",
OnSuccess = "doWork(this,'SomeCustomText')"
}))
{
<div id="container">
<--Some HTML--!>
<input type="submit" value="OK"/>
</div>
}
<script>
function doWork(e,customText)
{
alert(customText); //It shows 'SomeCustomText', so its good.
alert(e); //[Object] object
alert(e.prop("tagName")); //Object #<Object> has no method 'prop'
alert(e.attr("tagName")); //Object #<Object> has no method 'attr'
alert(jQuery(e).html()); //undefined
alert(jQuery(e).prop("tagName")); //undefined
alert(e.target); //undefined
alert(jQuery(e).target); //undefined
}
<script/>
Run Code Online (Sandbox Code Playgroud)
问题:
如何获得目标?!谢谢
jQuery版本应该如下所示:
jQuery.ajax({
url:"/Home/InsertModel",
data:"some post data",
type:"POST",
success:function(data){
doWork(this,data); // so …Run Code Online (Sandbox Code Playgroud) 我需要你的帮助.我正在使用AjaxBeginform在一个小文本字段中提交问题.我可以提交问题,它可以很好地发布到db,但问题永远不会解决.我正在使用Ajax,因为我不希望整个页面发布.我尝试过使用this.rest(); 但这适用于IE,但它不适用于Firefox和Chrome.我试过$('#question').reset(); 但那还是行不通的.我确信这是我做错了.
这是我的代码如下.谢谢您的帮助.
这是我在页面顶部的脚本:
<script type="text/javascript" src="~/Scripts/jquery-migrate- 1.2.1.min.js"></script>
Run Code Online (Sandbox Code Playgroud)
这是AjaxBeginForm以及文本框
@using (Ajax.BeginForm("SendQuestion", new { PresId = Model.PresentationID, catalogId = ViewBag.CatalogId }, new AjaxOptions
{
HttpMethod = "Post",
UpdateTargetId = "questionTxt",
OnBegin = "OnBegin",
OnSuccess = "OnSuccess",
OnFailure = "OnFailure"
}))
{
<fieldset id="question">
<p>
<textarea id="questionTxt" style="font-size:12px" cols="20" rows="6" name="questionTxt" onclick="javascript:removeTextAreaWhiteSpace(this);"></textarea>
</p>
<button id="question-btn" type="submit">Submit</button>
</fieldset>
}
Run Code Online (Sandbox Code Playgroud)
这是我对OnSuccess的功能
function OnSuccess() {
alert("Your question has been submitted.");
$('#question').val('');
}
Run Code Online (Sandbox Code Playgroud)
这是我的控制器
public bool SendQuestion(string questionTxt, long PresId, long catalogId)
{
try
{ …Run Code Online (Sandbox Code Playgroud) $ .ajaxSetup用于jquery中的ajax调用.如果我们使用jquery ajax,这完全正常.
但是在MVC中我们使用Ajax.BeginForm().回调处理程序是OnFailure,OnSuccess,OnBegin.
有没有办法可以将$ .ajaxSetup用于Ajax.BeginForm().
更新
例如,我在10个地方使用过Ajax.BeginForm(),但不是为所有人编写OnFailure处理程序,我只想在jquery $ .ajaxSetup中共同编写一个常见的OnFailure.有办法吗?
@Html.ValidationSummary() 在Ajax.BeginForm表单中使用是否有任何问题?
我有以下场景,我无法获得必填字段的验证.表单刚刚发布,也没有错误.
这是视图:
@using (Ajax.BeginForm("Register", "Account", new AjaxOptions { HttpMethod = "POST", OnSuccess = "closeDialog('RegistroUsuario')" }))
{
@Html.ValidationSummary()
<fieldset>
<legend>Cadastro novo Usuário</legend>
<table id="changePassword">
<tr>
<td class="smallField">Username:</td>
<td>@Html.TextBoxFor(m => m.UserName)</td>
</tr>
<tr>
<td>Password:</td>
<td>@Html.PasswordFor(m => m.Password)</td>
</tr>
<tr>
<td>Repetir Senha:</td>
<td>@Html.PasswordFor(m => m.ConfirmPassword)</td>
</tr>
<tr>
<td>Email:</td>
<td>@Html.TextBoxFor(m => m.Email)</td>
</tr>
<tr>
<td>Pergunta Secreta:</td>
<td>@Html.TextBoxFor(m => m.SecretQuestion)</td>
</tr>
<tr>
<td>Resposta:</td>
<td>@Html.TextBoxFor(m => m.SecretQuestionPassword)</td>
</tr>
<tr>
<td>Ativo:</td>
<td><input type="checkbox" name="status" id="status" value="Ativo"></td>
</tr>
</table>
</fieldset>
<input type="submit" value="Criar Usuário" …Run Code Online (Sandbox Code Playgroud) ajax.beginform ×10
asp.net-mvc ×4
ajax ×2
c# ×2
html ×1
insertion ×1
javascript ×1
jquery ×1
razor ×1
replace ×1
validation ×1