ASP.NET MVC如何使用Ajax.NET?
我尝试使用Asp.NETMVC4Ajax帮助程序,当我提交表单时,它会返回完整的帖子 注意,我在head元素中包含了所有重要的脚本,如Jquery和Jquery.unobtrusive-ajaxASP.NET MVC如何使用Ajax.NET?,asp.net,ajax,asp.net-mvc-4,asp.net-ajax,ajax.beginform,Asp.net,Ajax,Asp.net Mvc 4,Asp.net Ajax,Ajax.beginform,我尝试使用Asp.NETMVC4Ajax帮助程序,当我提交表单时,它会返回完整的帖子 注意,我在head元素中包含了所有重要的脚本,如Jquery和Jquery.unobtrusive-ajax <script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script> <script src="@Url.Content("
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery-1.9.1.min.js")" type="text/javascript"> </script>
学生管理员
[HttpPost]
public string Create(Students students)
{
if (Request.IsAjaxRequest())
{
if (ModelState.IsValid)
{
db.Students.Add(students);
db.SaveChanges();
// return RedirectToAction("Index");
}
}
return "<h2>Customer updated successfully!</h2>";
}
[HttpPost]
创建公共字符串(学生)
{
if(Request.IsAjaxRequest())
{
if(ModelState.IsValid)
{
db.Students.Add(学生);
db.SaveChanges();
//返回操作(“索引”);
}
}
返回“客户更新成功!”;
}
创建视图
@using (Ajax.BeginForm("Create","Students", new AjaxOptions() { HttpMethod= "POST", UpdateTargetId = "fs1" }))
{
<fieldset id="fs1">
<legend>Students</legend>
<div class="editor-label">
@Html.LabelFor(model => model.ST_NAME)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ST_NAME)
@Html.ValidationMessageFor(model => model.ST_NAME)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.ST_BIRTH_DATE)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ST_BIRTH_DATE)
@Html.ValidationMessageFor(model => model.ST_BIRTH_DATE)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.ST_PHONE)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ST_PHONE)
@Html.ValidationMessageFor(model => model.ST_PHONE)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.ST_ADDR)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ST_ADDR)
@Html.ValidationMessageFor(model => model.ST_ADDR)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.ST_CLASS)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ST_CLASS)
@Html.ValidationMessageFor(model => model.ST_CLASS)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.ST_STAT)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ST_STAT)
@Html.ValidationMessageFor(model => model.ST_STAT)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.LAST_UPDATE_DATE)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.LAST_UPDATE_DATE)
@Html.ValidationMessageFor(model => model.LAST_UPDATE_DATE)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.CURRENT_CLASS_GRADE)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.CURRENT_CLASS_GRADE)
@Html.ValidationMessageFor(model => model.CURRENT_CLASS_GRADE)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.CURRENT_CLASS)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.CURRENT_CLASS)
@Html.ValidationMessageFor(model => model.CURRENT_CLASS)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.ST_CODE)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ST_CODE)
@Html.ValidationMessageFor(model => model.ST_CODE)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.REG_CLASS)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.REG_CLASS)
@Html.ValidationMessageFor(model => model.REG_CLASS)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.IDNO)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.IDNO)
@Html.ValidationMessageFor(model => model.IDNO)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
@使用(Ajax.BeginForm(“创建”、“学生”、新的AjaxOptions(){HttpMethod=“POST”,UpdateTargetId=“fs1”}))
{
学生
@LabelFor(model=>model.ST_NAME)
@EditorFor(model=>model.ST_NAME)
@Html.ValidationMessageFor(model=>model.ST_NAME)
@LabelFor(model=>model.ST\u出生日期)
@EditorFor(model=>model.ST\u出生日期)
@Html.ValidationMessageFor(model=>model.ST\u出生日期)
@LabelFor(model=>model.ST_PHONE)
@EditorFor(model=>model.ST_PHONE)
@Html.ValidationMessageFor(model=>model.ST_PHONE)
@LabelFor(model=>model.ST_ADDR)
@EditorFor(model=>model.ST_ADDR)
@Html.ValidationMessageFor(model=>model.ST_ADDR)
@LabelFor(model=>model.ST_类)
@EditorFor(model=>model.ST_类)
@Html.ValidationMessageFor(model=>model.ST_类)
@LabelFor(model=>model.ST_STAT)
@EditorFor(model=>model.ST_STAT)
@Html.ValidationMessageFor(model=>model.ST_STAT)
@LabelFor(model=>model.LAST\u UPDATE\u DATE)
@EditorFor(model=>model.LAST\u UPDATE\u DATE)
@Html.ValidationMessageFor(model=>model.LAST\u UPDATE\u DATE)
@LabelFor(model=>model.CURRENT\u CLASS\u GRADE)
@Html.EditorFor(model=>model.CURRENT\u CLASS\u GRADE)
@Html.ValidationMessageFor(model=>model.CURRENT\u CLASS\u GRADE)
@LabelFor(model=>model.CURRENT_类)
@EditorFor(model=>model.CURRENT_类)
@Html.ValidationMessageFor(model=>model.CURRENT_类)
@LabelFor(model=>model.ST_代码)
@EditorFor(model=>model.ST_代码)
@Html.ValidationMessageFor(model=>model.ST_代码)
@LabelFor(model=>model.REG_类)
@EditorFor(model=>model.REG_类)
@Html.ValidationMessageFor(model=>model.REG_类)
@LabelFor(model=>model.IDNO)
@EditorFor(model=>model.IDNO)
@Html.ValidationMessageFor(model=>model.IDNO)
}
请注意,当我检查请求是否为ajax时,它给了我false。您是否尝试过翻转加载javascript的顺序?脚本是同步加载的,因此jquery应该在不引人注目的ajaxThank的SeanCarroll之前定义:)