12

我想将模型从表单传递到控制器并在同一视图中显示其他模型。我怎样才能做到这一点?我的主要问题是:如何发送到 testAcc actionresult、模型 CommentModel 和显示文本ViewData["Success"]

这是我的代码:

    @model XYZ.Models._ideaDetailsWrapper
    @using XYZ.Tools   
<article>
    <h2>@Model.idea.Tilte</h2>

<table><tr><td>
<p>
    Author: <b>@UserTools.getUser(Model.idea.AuthorID).UserName</b><br />
    Add date: @Model.idea.AddDate<br />
    Category: @IdeasTools.getCategoryName(Model.idea.CategoryID)<br />
</p></td>
</tr></table>

<p><b>Content:</b><br />
    @Model.idea.Content</p>

<br /><br />

// HERE - ADD comment

    @using (Html.BeginForm("testAcc", "Ideas", FormMethod.Post))
    { 
            <h4>Add comment:</h4>

            @Html.LabelFor(m => m.addComment.Content)
            @Html.EditorFor(m => m.addComment.Content)<br />
        <input type="submit" value="SendEmails" />
    }

@ViewData["Success"]

包装:

public class _ideaDetailsWrapper 
{
    public Ideas idea { get; set; }
    public IEnumerable<IdeasComment> commentList { get; set; }
    public CommentModel addComment { get; set; }
}

动作方法:

    [HttpPost]
    [Authorize]
    public ActionResult testAcc(CommentModel model)
    {

        CommentModel abs = model;

        ViewData["Success"] = "Working!";

        // ADD TO DATABASE, but model is null..
        return RedirectToAction("Details", "Ideas");
    }
4

1 回答 1

16

一种方法是使用Partial View

详细信息.cshtml

@model XYZ.Models._ideaDetailsWrapper
...
// HERE - ADD Comment
<div id="comment-form">
@Html.Partial("_CommentForm", Model.addComment)
</div>

@Model.message
// add validation javascript to this view

_CommentForm.cshtml(部分视图)

@model XYX.Models.CommentModel
@{
    Layout = null;
}

@using (Html.BeginForm("testAcc", "Ideas", FormMethod.Post))
{ 
      @Html.ValidationSummary(true)
        <h4>Add comment:</h4>

        @Html.LabelFor(m => m.Content)
        @Html.EditorFor(m => m.Content)
        @Html.ValidationMessageFor(m => m.Content)<br />
    <input type="submit" value="SendEmails" />
}

部分视图是强类型的,将提交CommentModel

行动方法:

[HttpPost]
[Authorize]
public ActionResult testAcc(CommentModel model)
{
    string abs = model.Content;

    TempData["Message"] = "Working!";

    // ADD TO DATABASE
    return RedirectToAction("Details", "Ideas", new { id = model.Idea.Id });
}

[HttpGet]
[Autorize]
public ActionResult Details(int id)
{
    var ideaModel = dbStore.GetIdea(id);  // however you do this

    var model = new _ideaDetailsWrapper {
        idea = ideaModel,
        addComment = new CommentModel(),
        message = TempData["Message"]
        ...
    };
    return View(model);
}

用于TempData通过重定向传递消息。当您在使用页面之前直接加载页面时,您将检查TempData["Message"]该操作是否存在。Details

编辑:对于验证,只需将验证 javascript 添加到详细信息视图和ValidationSummary部分视图。

编辑 2:此方法因验证和错误处理而失效。为此,它需要 AJAX 替换表单 div 而无需重新加载整个页面。

您需要拦截正常的表单提交并使用AJAX自己处理

$("form").on("submit", function(event) {
    event.preventDefault();
    $.ajax({
        url: "/Ideas/testAcc",
        type: "POST",
        data: $("form").serialize()
    })
    .done(function(partialViewHtml) {
        $("#comment-form").html(partialViewHtml);
    });
});

你的动作变成

[HttpPost]
public ActioNResult testAcc(CommentModel model)
{
    if (ModelState.IsValid)
    {
        ...
        return RedirectToAction("Details", "Ideas", new { id = model.Idea.Id });
    }
    return PartialView("_CommentForm", model);
}
于 2013-04-20T23:44:59.707 回答