.NET MVC中如何将Event ID从视图传递到分部视图再到控制器
在.NET MVC中传递Event ID到评论分部视图及控制器动作
我在.NET MVC Web应用中,需要将Event模型的ID从事件详情视图传递到用于评论该事件的分部视图(通过Ajax渲染),再传递到分部视图的Post控制器动作,为评论分配EventId以建立数据库关联。视图与分部视图使用不同模型,请问该如何实现?
工作流
- 视图(详情页,模型1:Event)→ 分部视图(创建评论,模型2:Comment)→ 控制器(CommentController的Create动作)
- Event.Id → 传递到分部视图 → 提交评论时,将ID发送到Create动作
详情视图代码(修改后)
@using Evented.Domain.Models @using Microsoft.AspNetCore.Identity @model Evented.Web.EventVMCompany @inject UserManager<User> UserManager @{ ViewData["Title"] = "Detail"; } <h1>Detail</h1> <h4>EventVM</h4> <hr /> @Html.ValidationMessageFor(m => m.Id) <form asp-action="JoinEvent"> <div> <dl class="row"> <dt class="col-sm-2"> @Html.DisplayNameFor(model => model.Id) </dt> <dd class="col-sm-10"> @Html.DisplayFor(model => model.Id) @Html.HiddenFor(model=>model.Id) </dd> <dt class="col-sm-2"> @Html.DisplayNameFor(model => model.Topic) @Html.HiddenFor(model=>model.Topic) </dt> <dd class="col-sm-10"> @Html.DisplayFor(model => model.Topic) </dd> <dt class="col-sm-2"> @Html.DisplayNameFor(model => model.Title) </dt> <dd class="col-sm-10"> @Html.DisplayFor(model => model.Title) @Html.HiddenFor(model=>model.Title) </dd> <dt class="col-sm-2"> @Html.DisplayNameFor(model => model.Description) @Html.HiddenFor(model=>model.Description) </dt> <dd class="col-sm-10"> @Html.DisplayFor(model => model.Description) </dd> <dt class="col-sm-2"> @Html.DisplayNameFor(model => model.Location) </dt> <dd class="col-sm-10"> @Html.DisplayFor(model => model.Location) @Html.HiddenFor(model=>model.Location) </dd> <dt class="col-sm-2"> @Html.DisplayNameFor(model => model.BeginsAt) </dt> <dd class="col-sm-10"> @Html.DisplayFor(model => model.BeginsAt) @Html.HiddenFor(model=>model.BeginsAt) </dd> <dt class="col-sm-2"> @Html.DisplayNameFor(model => model.EndsAt) </dt> <dd class="col-sm-10"> @Html.DisplayFor(model => model.EndsAt) @Html.HiddenFor(model=>model.EndsAt) </dd> <dt class="col-sm-2"> @Html.DisplayNameFor(model => model.joineeLimit ) </dt> <dd class="col-sm-10"> @Html.DisplayFor(model => model.joineeLimit) @Html.HiddenFor(model=>model.joineeLimit) </dd> @Html.HiddenFor(model=>model.CreatorId) @Html.HiddenFor(model=>model.joineeNumber) @Html.HiddenFor(model=>model.UsersJoined) @Html.HiddenFor(model=>model.UsersJoined.Count) </dl> </div> @{ if (Model.CreatorId != UserManager.GetUserId(User)) { @foreach (var item in Model.UsersJoined) { @if (item.UserId != UserManager.GetUserId(User)) { <button type="submit" class="btn btn-primary">Join</button> } else { <button type="submit" formaction="/Event/LeaveEvent" class="btn btn-primary">Leave</button> } } @if (Model.UsersJoined.Count == 0) { <button type="submit" class="btn btn-primary">Join</button> } } } </form> <a asp-action="Index" class="btn btn-primary">Back to List</a> <div>@{ foreach (var item in Model.UsersJoined) { if (item.UserId == UserManager.GetUserId(User)) { <div id="commentlist"></div> } } }</div> <div id="createcomment"></div> <script type="text/javascript"> // 直接传递当前事件的ID到评论分部视图 $('#createcomment').load('@Url.Action("Create","Comment", new { eventId = Model.Id })'); $('#commentlist').load('@Url.Action("Index","Comment", new { eventId = Model.Id })'); </script>
分部视图代码(修改后)
@model Evented.Web.CommentVM @{ // 接收从路由传递过来的EventId int eventId = ViewBag.EventId ?? 0; } <hr /> <div class="row"> <div class="col-md-4"> <form asp-action="Create" method="post"> <div asp-validation-summary="ModelOnly" class="text-danger"></div> <!-- 添加隐藏字段,提交表单时自动传递EventId --> <input type="hidden" name="eventId" value="@eventId" /> <div class="form-group"> <input asp-for="Text" class="form-control" placeholder="Comment"/> <span asp-validation-for="Text" class="text-danger"></span> </div> <div class="form-group"> <input name="button" id="button" type="submit" value="Comment" class="btn btn-primary " /> </div> </form> </div> </div>
控制器Create动作代码(修改后)
[HttpPost] [ValidateAntiForgeryToken] public async Task<ActionResult> Create(CommentVM commentVM, int eventId) { // 异步获取当前用户,避免同步阻塞 User usr = await usrManager.GetUserAsync(User); var comment = mapper.Map<Comment>(commentVM); comment.EventId = eventId; comment.User = usr; comment.CreatedAt = DateTime.Now; comment.UpdatedAt = DateTime.Now; await commentService.AddCommentAsync(comment); // 跳转回对应事件的详情页或评论列表 return RedirectToAction("Detail", "Event", new { id = eventId }); }
关键修改说明
- 详情视图Ajax请求:
原代码中ViewData["EventId"]未赋值,改为直接传递Model.Id作为路由参数,确保分部视图能获取到当前事件的ID。 - 分部视图存储EventId:
通过ViewBag.EventId接收路由参数,添加隐藏字段将EventId存入表单,提交时自动传递到控制器。 - 控制器动作优化:
将同步获取用户的方式改为异步调用,避免阻塞;跳转时携带EventId,确保回到对应事件的页面。
内容的提问来源于stack exchange,提问作者Furkan Koç
相关产品推荐
相关产品推荐

