我有一个名为'Details'的View,其中包含部分视图'PostReply'。我试图将Id从Details视图传递给PostReply Controller操作。
我在详细信息视图中尝试了这个,但它推送了id的null值。
@Html.Partial("PostReply", new XXXXX.Models.Reply{ QuestionId = Model.Question.ID })
然后我尝试在PostReply(部分视图)中为BeginForm添加一个id值,如下所示:
Html.BeginForm("PostReply", "Question", new { id = Model.Question.ID}))
这也行不通。如何从详细信息中获取id值到PostReply?提前致谢。
详细信息视图
@model XXXXXX.Models.ParentView.Questions
....
@Html.DisplayNameFor(model => model.QuestionModel.Title)
@Html.DisplayNameFor(model => model.QuestionModel.Desc)
....
@Html.ActionLink("Edit", "Edit", new { id = Model.QuestionModel.ID }) |
@Html.ActionLink("Back to List", "Index")
....
@Html.Partial("PostReply", new Chemtalks.Models.Reply())
PostReply(部分视图):
@model XXXXX.Models.Reply
@using (Html.BeginForm("PostReply", "Question", FormMethod.Post))
{
@Html.LabelFor(model => model.ReplyText)
@Html.EditorFor(model => model.ReplyText)
@Html.ValidationMessageFor(model => model.ReplyText
<input type="submit" value="Create" class="btn btn-default" />
}
详细信息&amp; PostReply控制器:
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
var model = new ParentView.Questions
{
QuestionModel = db.QuestionModels.Find(id),
Reply = db.Replies.Where(t => t.QuestionId == id)
};
if (model == null)
{
return HttpNotFound();
}
return View(model);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult PostReply([Bind(Include = "ReplyID, ReplyText, UserID, QuestionId")] Reply reply, int? id)
{
try
{
if (ModelState.IsValid)
{
reply.QuestionId = id;
reply.UserID = User.Identity.GetUserId();
db.Replies.Add(reply);
db.SaveChanges();
}
}
catch (DataException)
{
ModelState.AddModelError("", "Unable to post your reply. Please try again later.");
}
return PartialView(reply);
}
QuestionModel:
public class QuestionModels
{
public int ID { get; set; }
public string Title { get; set; }
public string Desc { get; set; }
public string UserID { get; set; }
[ForeignKey("QuestionId")]
public virtual ICollection<Reply> Replies { get; set; }
}
public class Reply
{
public int ReplyID { get; set; }
public string ReplyText { get; set; }
public string UserID { get; set; }
public int? QuestionId { get; set; }
public virtual QuestionModels QuestionsModels { get; set; }
}
ParentView模型:
public class ParentView
{
public class Questions
{
public QuestionModels QuestionModel { get; set; }
public IEnumerable<Reply> Reply { get; set; }
}
}
答案 0 :(得分:1)
您应该使用@Html.Action
方法呈现部分视图并为其运行子操作。 @Html.Partial
不会调用任何操作方法。
因此,子视图的渲染应该看起来像
@Html.Action("PostReply", new XXXXX.Models.Reply{ QuestionId = Model.Question.ID })
并且控制器中的子操作不适用于属性[HttpPost]
。最好将其与[ValidateAntiForgeryToken]
一起删除。
最好使用[ChildActionOnly]
属性的此类操作,以禁止因直接请求而调用操作。
<强>更新强> 对不起,我认为你需要从动作中获取一些数据来呈现视图,但在Stephen Muecke的评论之后我意识到你不需要它。所以,一般来说你应该使用斯蒂芬建议的
@Html.Partial("PostReply", new Chemtalks.Models.Reply { QuestionId = Model.QuestionModel.ID })
然后
Html.BeginForm("PostReply", "Question", new { QuestionId = Model.QuestionId })
或者您可以在PostReply表单中添加隐藏字段,而不是路由参数
@Html.HiddenFor(model => model.QuestionId)
如果使用错误的BeginForm
重载,则使用route参数的方法无效这一个
Html.BeginForm("PostReply", "Question", FormMethod.Post, new {QuestionId = Model.QuestionId})