【问题标题】:MVC3 - Ajax.BeginForm and partial view refreshesMVC3 - Ajax.BeginForm 和部分视图刷新
【发布时间】:2011-07-06 14:24:56
【问题描述】:

有没有办法刷新局部视图的一部分,或者我应该将该部分分解成它自己的单独局部视图?我面临的问题是,每当我提交表单时,整个部分视图都会更新,复制我的表单输入,但我真的只希望“noteList”更新。以下是一些可能有用的详细信息。

在我的项目中,我有一个页面被分成单独的选项卡,每个选项卡都是它自己的部分视图,当有人单击该选项卡时会加载该视图。我现在关注的是一个笔记标签。

下面是捕捉帖子的标记和控制器。

<div id="noteList">
    @Html.Grid(Model).Columns(column => {
        column.For(x => x.TimeStamp);
        column.For(x => x.UserName);
        column.For(x => x.Note);
        }).Attributes(Style => "text-aligh: center", @Class => "linkGrid")
</div>
<div id="addNoteForm">
    <h2>Add New Note</h2>
    @using (Ajax.BeginForm("AddNote", "Dispute", null, new AjaxOptions { UpdateTargetId = "noteList" }, new { id = "AddNote" })) {
        @Html.Hidden("DisputeID", ViewData["DisputeID"])
        <div class="editor-multiline-field">
            @Html.Editor("Note", "editor-multiline-field")
        </div>
        <input type="submit" value="Add Note" />        
    }
</div>

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult AddNote(FormCollection collection) {
    if (this.ModelState.IsValid) {
        DisputeNote newNote = new DisputeNote(repository.Get(int.Parse(collection["DisputeID"])), User.Identity.Name, collection["Note"]);
        repository.SaveForUpdate<DisputeNote>(newNote);
    }
    var Notes = repository.GetNotesForDispute(int.Parse(collection["DisputeID"]));
    ViewData["DisputeID"] = int.Parse(collection["DisputeID"]);
    return PartialView("NoteList", Notes);
}

我知道将它分成另一个部分视图会起作用,但我很好奇是否有其他方法可以做到这一点。

【问题讨论】:

    标签: c# asp.net-mvc-3 jquery


    【解决方案1】:

    您需要另一个局部视图才能按照您在此处列出的方式执行此操作 - 您无法使用相同的局部视图本身更新局部视图的一部分。但是 :) 还有其他选项 - 例如仅返回 JSON。 您可以使用 .ajax() 调用将 JSON 发布到您的控制器方法以添加注释。

    粗略的想法见这篇文章:

    JSON / MVC (3P1) HttpPost - not getting it to work on my EF class

    【讨论】:

    • 感谢您的回复!我会看看这种方法,看看它是否有帮助。我的主要动机是我的项目有很多源文件,浏览项目变得很麻烦。我过去做的大部分工作都是后端处理或简单的windows窗体,所以我习惯了较小的源文件集。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-09
    • 2013-01-25
    • 1970-01-01
    相关资源
    最近更新 更多