weixin_33719619 2014-01-30 08:15 采纳率: 0%
浏览 43

MVC 5 的 Ajax 问题?

我正在学习MVC 5,正在尝试实现一个简单的页面来添加和显示students。这个问题需要很大的篇幅,但却是极其基本的。

下面是模型:

public class Student
{
    public int StudentId { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
}

以下是action方法:

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>

这是子视图: Create.cshtml

@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>
}

现在我有几个问题:

  1. 当我键入学生姓名和年龄并单击创建时,文本框仍显示值而不是被清除。 数据也会被保存在数据库中。
  2. 如果我想在添加新学生后立即更新左侧的列表,该怎么办?

我对MVC4有一点经验,如果我(在AjaxOptions中)传递了包含两个子div(在我的情况下为“ myformbase”)的div的ID,那么它将用于更新它。但不确定为什么这在MVC5中不起作用。

  • 写回答

2条回答 默认 最新

  • weixin_33725239 2014-01-30 08:24
    关注
    1. Just add ModelState.Clear(); after you have saved record in to database;
    2. Try to change UpdateTargetId to the base container ID:

      @using (Ajax.BeginForm("Create", "Student", new AjaxOptions { UpdateTargetId = "myformbase" })){...}

    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)