MVC5Ajax问题
我正在学习MVC5,我正在尝试实现一个简单的页面来添加和显示学生。这个问题需要很大的篇幅,但非常基本 这就是模型:MVC5Ajax问题,ajax,asp.net-mvc,c#-4.0,unobtrusive-ajax,asp.net-mvc-5.1,Ajax,Asp.net Mvc,C# 4.0,Unobtrusive Ajax,Asp.net Mvc 5.1,我正在学习MVC5,我正在尝试实现一个简单的页面来添加和显示学生。这个问题需要很大的篇幅,但非常基本 这就是模型: public class Student { public int StudentId { get; set; } public string Name { get; set; } public int Age { get; set; } } 以下是行动方法: public ActionResult Index() { retur
public class Student
{
public int StudentId { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
以下是行动方法:
public ActionResult Index()
{
return View(db.Students.ToList());
}
public ActionResult Create()
{
return PartialView();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Student student)
{
if (ModelState.IsValid)
{
db.Students.Add(student);
db.SaveChanges();
return PartialView();
}
return PartialView(student);
}
以下是父视图:
Index.cshtml
@model IEnumerable<MVCAjax.Models.Student>
<h2>Index</h2>
<div class="row" id="myformbase">
<div class="col-md-6">
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Age)
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Age)
</td>
</tr>
}
</table>
</div>
<div class="col-md-6">
@Html.Action("Create")
</div>
</div>
@model MVCAjax.Models.Student
@using (Ajax.BeginForm("Create", "Student", new AjaxOptions { UpdateTargetId = "myform" }))
{
<div id="myform">
<h2>Create</h2>
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Student</h4>
<hr />
@Html.ValidationSummary(true)
<div class="form-group">
@Html.LabelFor(model => model.Name, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Age, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Age)
@Html.ValidationMessageFor(model => model.Age)
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
</div>
}
@model IEnumerable
指数
@DisplayNameFor(model=>model.Name)
@DisplayNameFor(model=>model.Age)
@foreach(模型中的var项目)
{
@DisplayFor(modelItem=>item.Name)
@DisplayFor(modelItem=>item.Age)
}
@动作(“创建”)
下面是子视图:Create.cshtml
@model IEnumerable<MVCAjax.Models.Student>
<h2>Index</h2>
<div class="row" id="myformbase">
<div class="col-md-6">
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Age)
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Age)
</td>
</tr>
}
</table>
</div>
<div class="col-md-6">
@Html.Action("Create")
</div>
</div>
@model MVCAjax.Models.Student
@using (Ajax.BeginForm("Create", "Student", new AjaxOptions { UpdateTargetId = "myform" }))
{
<div id="myform">
<h2>Create</h2>
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Student</h4>
<hr />
@Html.ValidationSummary(true)
<div class="form-group">
@Html.LabelFor(model => model.Name, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Age, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Age)
@Html.ValidationMessageFor(model => model.Age)
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
</div>
}
@model MVCAjax.Models.Student
@使用(Ajax.BeginForm(“创建”、“学生”、新的AjaxOptions{UpdateTargetId=“myform”}))
{
创造
@Html.AntiForgeryToken()
学生
@Html.ValidationSummary(true)
@LabelFor(model=>model.Name,新的{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Name)
@Html.ValidationMessageFor(model=>model.Name)
@LabelFor(model=>model.Age,新的{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Age)
@Html.ValidationMessageFor(model=>model.Age)
}
现在我有几个问题:
ModelState.Clear()代码>将记录保存到数据库后李>
尝试将UpdateTargetId更改为基本容器ID:
@使用(Ajax.BeginForm(“创建”、“学生”、新的AjaxOptions{UpdateTargetId=“myformbase”})){…}
当我键入学生姓名和年龄并单击“创建”时,文本框
仍然显示值,而不是清除。数据保存在
db
解决这个问题的一个正确方法是做一个测试。或者,您可以创建一个空模型并将其与视图绑定(我的意思是Student s=new Student();返回PartialView;
)
如果我想尽快更新左边的列表怎么办
学生被添加
在这种情况下,PRG也会解决您的问题。所以,一旦创建完成,重定向到索引操作。或者,您可以使用AJAX获取索引操作并在视图中加载html。谢谢。它解决了我的第一个问题。你知道第二个吗?