asp.net-mvc 如何在MVC视图中传递模型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16126049/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to pass model in MVC view
提问by whoah
I want to pass model from form to controler and show others models in the same view. How can I do this? My main problem is: how to send into testAcc actionresult, model CommentModel, and show text in ViewData["Success"]?
我想将模型从表单传递给控制器并在同一视图中显示其他模型。我怎样才能做到这一点?我的主要问题是:如何发送到 testAcc actionresult,模型 CommentModel,并在ViewData["Success"]?
Here is my code:
这是我的代码:
@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"]
wrapper:
包装器:
public class _ideaDetailsWrapper
{
public Ideas idea { get; set; }
public IEnumerable<IdeasComment> commentList { get; set; }
public CommentModel addComment { get; set; }
}
Action method:
动作方法:
[HttpPost]
[Authorize]
public ActionResult testAcc(CommentModel model)
{
CommentModel abs = model;
ViewData["Success"] = "Working!";
// ADD TO DATABASE, but model is null..
return RedirectToAction("Details", "Ideas");
}
回答by Jasen
One way to do this is to use a Partial View.
一种方法是使用Partial View。
Details.cshtml
详情.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(Partial 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" />
}
The partial view is strongly typed and will submit the CommentModel
局部视图是强类型的,将提交 CommentModel
Action methods:
动作方法:
[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);
}
Use TempDatato pass the message through redirect. You'll check if TempData["Message"]exists in the Detailsaction when you first load the page directly before you use it.
用于TempData通过重定向传递消息。在使用它之前,当您第一次直接加载页面时,您将检查操作中是否TempData["Message"]存在Details。
Edit:For Validation just add the validation javascript to the Details view and the ValidationSummaryto the partial view.
编辑:对于验证,只需将验证 javascript 添加到详细信息视图和ValidationSummary部分视图。
Edit 2:This method breaks down with validation and error handling. For this to work it needs AJAX to replace the form div without reloading the entire page.
编辑 2:此方法分解为验证和错误处理。为此,它需要 AJAX 来替换表单 div 而不重新加载整个页面。
You need to intercept the normal form submission and handle it yourself using AJAX
需要拦截正常的表单提交,使用AJAX自己处理
$("form").on("submit", function(event) {
event.preventDefault();
$.ajax({
url: "/Ideas/testAcc",
type: "POST",
data: $("form").serialize()
})
.done(function(partialViewHtml) {
$("#comment-form").html(partialViewHtml);
});
});
Your action becomes
你的行动变成
[HttpPost]
public ActioNResult testAcc(CommentModel model)
{
if (ModelState.IsValid)
{
...
return RedirectToAction("Details", "Ideas", new { id = model.Idea.Id });
}
return PartialView("_CommentForm", model);
}

