C# BiginForm导致回发。我有两个Ajax脚本,也没有嵌套的表单
我正在尝试使用Ajax.BefinForm(),并尝试了网络上的所有可用方法以避免回复帖子。我就是想不出我做错了什么。请帮我做这个 主要观点:C# BiginForm导致回发。我有两个Ajax脚本,也没有嵌套的表单,c#,ajax.beginform,C#,Ajax.beginform,我正在尝试使用Ajax.BefinForm(),并尝试了网络上的所有可用方法以避免回复帖子。我就是想不出我做错了什么。请帮我做这个 主要观点: @model GenericApp.Models.ExperienceModel <script src="@Url.Content("~/Scripts/MicrosoftAjax.js")" type="text/javascript"></script> <script src="@Url.Content("~/
@model GenericApp.Models.ExperienceModel
<script src="@Url.Content("~/Scripts/MicrosoftAjax.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/MicrosoftMvcAjax.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@{
ViewBag.Title = "Details";
var comments = Model.Comments == null ? new GenericApp.Models.Comments() : Model.Comments;
//comments.
}
<h2>Details</h2>
<p>
@Html.ActionLink("Edit", "Edit", new { id = Model.ID }) |
@Html.ActionLink("Back to List", "Index")
</p>
<fieldset>
<legend>ExperienceModel</legend>
<div class="display-field">
<h2>
@Html.DisplayFor(model => model.Title)
</h2>
</div>
<div class="display-field">
@Html.DisplayFor(model => model.Description)
</div>
<div class="display-field">
<b>Created By:</b> @Html.DisplayFor(model => model.CreatedBy)
</div>
<div class="display-field">
<b>Create Date:</b> @Html.DisplayFor(model => model.CreateDate)
</div>
<div class="display-field">
<b>Modified By:</b> @Html.DisplayFor(model => model.ModifiedBy)
</div>
<div class="display-field">
<b>Modified Date:</b> @Html.DisplayFor(model => model.ModifiedDate)
</div>
</fieldset>
@if (User.Identity.Name == null || User.Identity.Name.Length == 0)
{
<div>To view or add comments, please log-in</div>
}
else
{
<div id="ShowAllComments">
@Html.Partial("_ShowComments", comments)
</div>
}
@model GenericApp.Models.ExperienceModel
@{
ViewBag.Title=“详细信息”;
var comments=Model.comments==null?新的GenericApp.Models.comments():Model.comments;
//评论。
}
细节
@ActionLink(“编辑”,“编辑”,新的{id=Model.id})|
@ActionLink(“返回列表”、“索引”)
经验模型
@DisplayFor(model=>model.Title)
@DisplayFor(model=>model.Description)
创建人:@Html.DisplayFor(model=>model.CreatedBy)
创建日期:@Html.DisplayFor(model=>model.CreateDate)
修改人:@Html.DisplayFor(model=>model.ModifiedBy)
修改日期:@Html.DisplayFor(model=>model.ModifiedDate)
@if(User.Identity.Name==null | | User.Identity.Name.Length==0)
{
要查看或添加评论,请登录
}
其他的
{
@Html.Partial(“\u showcoments”,comments)
}
局部视图“\u ShowComments”
@model GenericApp.Models.Comments
描述
创造的
创建时间
@foreach(Model.CommentList中的变量项)
{
@DisplayFor(modelItem=>item.Description)
@DisplayFor(modelItem=>item.CreatedBy)
@DisplayFor(modelItem=>item.CreateDate)
}
@{
GenericApp.Models.Comment Comment=新的GenericApp.Models.Comment();
AjaxOptions opts=新的AjaxOptions()
{
UpdateTargetId=“ShowAllComments”,
HttpMethod=“POST”,
确认=“是否确实要保存您的评论”,
InsertionMode=InsertionMode.Replace
};
使用(Ajax.BeginForm(“StoreComments”,new{id=Model.ParentID},opts,new{id=“ajaxForm”}))
{
Html.ValidationSummary(true);
@LabelFor(model=>comment.Description)
@EditorFor(model=>comment.Description)
@Html.ValidationMessageFor(model=>comment.Description)
}
}
我的控制器中的操作代码:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult StoreComments(int id, FormCollection collection)
{
if (Request.IsAjaxRequest())
{
ExperienceModel exp = getExpById(id);
if (exp.Comments == null)
{
exp.Comments = new Comments();
exp.Comments.ParentID = exp.ID;
exp.Comments.CommentList = new List<Comment>();
}
Comment com = new Comment();
com.Description = Request.Params["comment.Description"];
com.CreatedBy = User.Identity.Name;
com.CreateDate = new DateTime().ToString();
com.ID = exp.Comments.CommentList.Count;
exp.Comments.CommentList.Add(com);
return PartialView("_ShowComments", exp.Comments);
}
else
{
return this.Content("Hi");
}
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult StoreComments(int id,FormCollection集合)
{
if(Request.IsAjaxRequest())
{
ExperienceModel exp=getExpById(id);
如果(exp.Comments==null)
{
exp.Comments=新注释();
exp.Comments.ParentID=exp.ID;
exp.Comments.CommentList=新列表();
}
Comment com=新注释();
com.Description=Request.Params[“comment.Description”];
com.CreatedBy=User.Identity.Name;
com.CreateDate=new DateTime().ToString();
com.ID=exp.Comments.CommentList.Count;
exp.Comments.CommentList.Add(com);
返回PartialView(“\u ShowComments”,exp.Comments);
}
其他的
{
返回此.Content(“Hi”);
}
}
我总是在空白页上看到“嗨”
呈现的HTML:
<!DOCTYPE html>
<html>
<head>
<title>Details</title>
<link href="/Content/Site.css" rel="stylesheet" type="text/css" />
<script src="/Scripts/jquery-1.5.1.min.js" type="text/javascript"></script>
</head>
<body>
<div class="page">
<div id="header">
<div id="title">
<h1>My MVC Application</h1>
</div>
<div id="logindisplay">
Welcome <strong>sadanands</strong>!
[ <a href="/Account/LogOff">Log Off</a> ]
</div>
<div id="menucontainer">
<ul id="menu">
<li><a href="/">Home</a></li>
<li><a href="/Home/About">About Us</a></li>
<li><a href="/Experience">Our Experience</a></li>
<li></li>
</ul>
</div>
</div>
<div id="main">
<script src="/Scripts/MicrosoftAjax.js" type="text/javascript"></script>
<script src="/Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>
<script src="/Scripts/jquery.validate.min.js" type="text/javascript"></script>
<script src="/Scripts/jquery.validate.unobtrusive.min.js" type="text/javascript"></script>
<h2>Details</h2>
<p>
<a href="/Experience/Edit/1">Edit</a> |
<a href="/Experience">Back to List</a>
</p>
<fieldset>
<legend>ExperienceModel</legend>
<div class="display-field">
<h2>
First Exp
</h2>
</div>
<div class="display-field">
hsdh asfhasjfkl fidsjfkldsj fkl dklds lfjdslfj sdsfhkjhdsf ahdfklhs fdsfhklds fldshfklsd fdskfhklds fkjds fhdskfhs fds fkfhkjds ffhsdk
</div>
<div class="display-field">
<b>Created By:</b> xxxxxxx
</div>
<div class="display-field">
<b>Create Date:</b> 1/1/2010
</div>
<div class="display-field">
<b>Modified By:</b> abcdefg
</div>
<div class="display-field">
<b>Modified Date:</b> 1/5/2010
</div>
</fieldset>
<div id="ShowAllComments">
<table>
<tr>
<th>
Description
</th>
<th>
CreatedBy
</th>
<th>
CreateDate
</th>
</tr>
</table>
<form action="/Experience/StoreComments/0" data-ajax="true" data-ajax-confirm="Are you sure you want to save your comments" data-ajax-method="POST" data-ajax-mode="replace" data-ajax-update="#ShowAllComments" id="ajaxForm" method="post"> <fieldset>
<div class="editor-field">
<label for="comment_Description">Description</label></div>
<div class="editor-field">
<textarea class="text-box multi-line" data-val="true" data-val-required="The Description field is required." id="comment_Description" name="comment.Description">
</textarea>
<span class="field-validation-valid" data-valmsg-for="comment.Description" data-valmsg-replace="true"></span>
</div>
</fieldset>
<input type="submit" value="Save" />
</form>
</div>
</div>
<div id="footer">
</div>
</div>
</body>
</html>
细节
我的MVC应用程序
欢迎来到萨达南德!
[ ]
细节
|
经验模型
第一经验
hsdh asfhasjfkl fidsjfkldsj fkl dklds lfjdslfj sdsfhkjhdsf ahdfklhs fdsfhklds fldshfklsd fdskfhklds fkjds FHDSKFS fds FKFKJDS ffhsdk
创建人:xxxxxxx
创建日期:2010年1月1日
修改人:abcdefg
修改日期:2010年1月5日
描述
创造的
创建时间
描述
我在页面上没有看到任何javascript错误。我确实使用IE调试器工具来检查MicrosoftJax.js和MicrosoftMvcAjax.js是否正在加载,它们确实在加载
任何帮助都将受到高度赞赏
“视图”文件夹中的My Web.Config:
希望我已经把所有的信息都放在这里了 我已经找到了答案 在视图中,除了一个脚本之外,我包含了所有必需的脚本 如果你看一下我之前帖子中的主视图,它已经
<script src="@Url.Content("~/Scripts/MicrosoftAjax.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/MicrosoftMvcAjax.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
但缺少的是:
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>
添加了这个脚本标记后,我想您可以去掉MicrosoftJax.js和MicrosoftMvcAjax.js,代码仍然可以工作。对我来说是这样的
萨达南德
<script src="@Url.Content("~/Scripts/MicrosoftAjax.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/MicrosoftMvcAjax.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>