开发者

MVC3 - Ajax.BeginForm and partial view refreshes

Is there a way to refresh a section of a partial view, or should I break that section out into it's own separate partial view? The problem I'm facing is that whenever I submit my form, the entire partial view updates, duplicating my form inputs, but I really only want the "noteList" to update. Here is some开发者_如何学运维 more details that might be helpful.

In my project, I have a page broken out into separate tabs, each of them is it's own partial view that is loaded when someone clicks on that tab. The one I'm focusing on at the moment is a notes tab.

Below is the markup and controller that catches the post.

<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);
}

I know breaking it into another partial view will work, but I'm curious if there is another way to do it.


You need another partial view to do it the way you have listed here - you cannot update a portion of a partial view with that same partial view itself. HOWEVER : ) there are other options though - such as just returning JSON. You could just use a .ajax() call to post the JSON to your controller method to add the note.

See this post for the rough idea:

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜