私は MVC5 を学んでおり、生徒を追加して表示するための簡単なページを実装しようとしています。この質問は多くのスペースを必要としますが、非常に基本的なものです。
 
 
モデルは次のとおりです。
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>
これが子ビューです: 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>
}
今、私はいくつかの問題を抱えています:
- 生徒の名前と年齢を入力して [作成] をクリックすると、テキスト ボックスがクリアされずに値が表示されたままになります。データは db に保存されます。
- 新しい生徒が追加されたらすぐに左側のリストを更新したい場合はどうすればよいですか。
私は MVC4 の経験が少しあり、(AjaxOptions で) 両方の子 div (私の場合は「myformbase」) を含む div の ID を渡していた場合、それを更新していました。しかし、これが MVC5 で機能しない理由がわかりません。