Asp.net mvc 4 如何将ViewModel传递回同一页面
我正试图让一个HTTPPost为视频页面的评论部分工作,但在回发时遇到了一些困难。正在将保存记录到数据库中,但在重新加载页面时,我无法找到第一次找到的@Model.SelectedMediaItem.Name变量。我的html如下所示:Asp.net mvc 4 如何将ViewModel传递回同一页面,asp.net-mvc-4,razor,asp.net-mvc-viewmodel,html.beginform,Asp.net Mvc 4,Razor,Asp.net Mvc Viewmodel,Html.beginform,我正试图让一个HTTPPost为视频页面的评论部分工作,但在回发时遇到了一些困难。正在将保存记录到数据库中,但在重新加载页面时,我无法找到第一次找到的@Model.SelectedMediaItem.Name变量。我的html如下所示: @{ ViewBag.Title = "Screencast"; Layout = "~/Views/Shared/_Layout.cshtml"; } @model Project.ViewModels.MediaViewModel <body onlo
@{
ViewBag.Title = "Screencast";
Layout = "~/Views/Shared/_Layout.cshtml";
}
@model Project.ViewModels.MediaViewModel
<body onload="initialize()">
<div class="container region3wrap_screencast">
<div class="row content_top_contact">
<div class="nine columns">
<ul class="breadcrumbs">
<li><a href="@Url.Action("Index","Home")">Home</a></li>
<li><a href="@Url.Action("Index","Media")">Media</a></li>
<li><a href="@Url.Action("Screencast","Media")">Screencast</a></li>
<li class="current"><a href="#">@Model.SelectedMediaItem.Name</a></li>
</ul>
</div>
</div>
</div>
<div class="twelve columns leave-comment">
<h3>Leave a Comment</h3>
@using(Html.BeginForm("Screencast","Media", FormMethod.Post, Model))
{
<div class="row">
<div class="six columns">
<div class="row">
<div class="six columns">
@Html.LabelFor(c => c.FeedbackComment.UserID)
@Html.TextBoxFor(c => c.FeedbackComment.UserID)
</div>
<div class="six columns">
@Html.LabelFor(c => c.FeedbackComment.ContentID)
@Html.TextBoxFor(c => c.FeedbackComment.ContentID)
</div>
<div class="row">
<div class="twelve columns">
@Html.LabelFor(c => c.FeedbackComment.FeedbackString)
@Html.TextAreaFor(c => c.FeedbackComment.FeedbackString)
</div>
</div>
</div>
</div>
</div>
<input type="submit" value="Submit button" class="medium button bottom20"/>
}
</div>
</body>
//GET
public ActionResult Screencast(int ID)
{
mvm = new ViewModels.MediaViewModel(ID);
return View(mvm);
}
//POST
[HttpPost]
public ActionResult Screencast(MediaViewModel mvm)
{
Feedback newFeedback= new Feedback();
newFeedback.UserID = mvm.FeedbackComment.UserID;
newFeedback.ContentID = mvm.FeedbackComment.ContentID;
newFeedback.CourseID = null;
newFeedback.Timestamp = DateTime.Now;
newFeedback.FeedbackString = mvm.FeedbackComment.FeedbackString;
//Initialize the Feedback Repository and save changes
feedbackRepository = new Repository<Feedback>(dbcontext);
feedbackRepository.Add(newFeedback);
feedbackRepository.SaveChanges();
return View(mvm);
}
public class MediaViewModel
{
private Repository<Content> contentRepository;
private Repository<Feedback> feedbackRepository;
private MetaLearningContext dbcontext;
public int screencastID { get; set; }
public IEnumerable<Content> Screencasts { get; set; }
public IEnumerable<Content> Podcasts { get; set; }
public IEnumerable<Content> Documents { get; set; }
public IEnumerable<Feedback> FeedbackComments { get; set; }
public Content SelectedMediaItem { get; set; }
public MetaLearningUser User { get; set; }
public MetaLearningUser FeedbackAuthor { get; set; }
public Feedback FeedbackComment { get; set; }
public MediaViewModel()
{
this.dbcontext = new MyContext(System.Configuration.ConfigurationManager.ConnectionStrings["MyContext"].ConnectionString);
this.contentRepository = new Repository<Content>(dbcontext);
this.feedbackRepository = new Repository<Feedback>(dbcontext);
this.dbcontext.Configuration.LazyLoadingEnabled = true;
//Retrieve a list of Screencasts
Screencasts = contentRepository
.Get(c => c.ContentTypeID == 1);
//Retrieve a list of Podcasts
Podcasts = contentRepository
.Get(c => c.ContentTypeID == 2);
//Retrieve a list of Documents
Documents = contentRepository
.Get(c => c.ContentTypeID == 3);
}
public MediaViewModel(int id)
{
this.dbcontext = new MyContext(System.Configuration.ConfigurationManager.ConnectionStrings["MyContext"].ConnectionString);
this.contentRepository = new Repository<Content>(dbcontext);
this.feedbackRepository = new Repository<Feedback>(dbcontext);
this.dbcontext.Configuration.LazyLoadingEnabled = true;
//Retrieve a list of Screencasts
Screencasts = contentRepository
.Get(c => c.ContentTypeID == 1);
//Retrieve a list of Podcasts
Podcasts = contentRepository
.Get(c => c.ContentTypeID == 2);
//Retrieve a list of Documents
Documents = contentRepository
.Get(c => c.ContentTypeID == 3);
//Retrieve selected screencast
SelectedMediaItem = contentRepository
.Get(c => c.ContentID == id)
.FirstOrDefault();
}
}
@{
ViewBag.Title=“Screencast”;
Layout=“~/Views/Shared/_Layout.cshtml”;
}
@模型Project.ViewModels.MediaViewModel
留言
@使用(Html.BeginForm(“屏幕广播”,“媒体”,FormMethod.Post,Model))
{
@LabelFor(c=>c.FeedbackComment.UserID)
@Html.TextBoxFor(c=>c.FeedbackComment.UserID)
@LabelFor(c=>c.FeedbackComment.ContentID)
@TextBoxFor(c=>c.FeedbackComment.ContentID)
@LabelFor(c=>c.FeedbackComment.FeedbackString)
@Html.TextAreaFor(c=>c.FeedbackComment.FeedbackString)
}
我的控制器操作如下:
@{
ViewBag.Title = "Screencast";
Layout = "~/Views/Shared/_Layout.cshtml";
}
@model Project.ViewModels.MediaViewModel
<body onload="initialize()">
<div class="container region3wrap_screencast">
<div class="row content_top_contact">
<div class="nine columns">
<ul class="breadcrumbs">
<li><a href="@Url.Action("Index","Home")">Home</a></li>
<li><a href="@Url.Action("Index","Media")">Media</a></li>
<li><a href="@Url.Action("Screencast","Media")">Screencast</a></li>
<li class="current"><a href="#">@Model.SelectedMediaItem.Name</a></li>
</ul>
</div>
</div>
</div>
<div class="twelve columns leave-comment">
<h3>Leave a Comment</h3>
@using(Html.BeginForm("Screencast","Media", FormMethod.Post, Model))
{
<div class="row">
<div class="six columns">
<div class="row">
<div class="six columns">
@Html.LabelFor(c => c.FeedbackComment.UserID)
@Html.TextBoxFor(c => c.FeedbackComment.UserID)
</div>
<div class="six columns">
@Html.LabelFor(c => c.FeedbackComment.ContentID)
@Html.TextBoxFor(c => c.FeedbackComment.ContentID)
</div>
<div class="row">
<div class="twelve columns">
@Html.LabelFor(c => c.FeedbackComment.FeedbackString)
@Html.TextAreaFor(c => c.FeedbackComment.FeedbackString)
</div>
</div>
</div>
</div>
</div>
<input type="submit" value="Submit button" class="medium button bottom20"/>
}
</div>
</body>
//GET
public ActionResult Screencast(int ID)
{
mvm = new ViewModels.MediaViewModel(ID);
return View(mvm);
}
//POST
[HttpPost]
public ActionResult Screencast(MediaViewModel mvm)
{
Feedback newFeedback= new Feedback();
newFeedback.UserID = mvm.FeedbackComment.UserID;
newFeedback.ContentID = mvm.FeedbackComment.ContentID;
newFeedback.CourseID = null;
newFeedback.Timestamp = DateTime.Now;
newFeedback.FeedbackString = mvm.FeedbackComment.FeedbackString;
//Initialize the Feedback Repository and save changes
feedbackRepository = new Repository<Feedback>(dbcontext);
feedbackRepository.Add(newFeedback);
feedbackRepository.SaveChanges();
return View(mvm);
}
public class MediaViewModel
{
private Repository<Content> contentRepository;
private Repository<Feedback> feedbackRepository;
private MetaLearningContext dbcontext;
public int screencastID { get; set; }
public IEnumerable<Content> Screencasts { get; set; }
public IEnumerable<Content> Podcasts { get; set; }
public IEnumerable<Content> Documents { get; set; }
public IEnumerable<Feedback> FeedbackComments { get; set; }
public Content SelectedMediaItem { get; set; }
public MetaLearningUser User { get; set; }
public MetaLearningUser FeedbackAuthor { get; set; }
public Feedback FeedbackComment { get; set; }
public MediaViewModel()
{
this.dbcontext = new MyContext(System.Configuration.ConfigurationManager.ConnectionStrings["MyContext"].ConnectionString);
this.contentRepository = new Repository<Content>(dbcontext);
this.feedbackRepository = new Repository<Feedback>(dbcontext);
this.dbcontext.Configuration.LazyLoadingEnabled = true;
//Retrieve a list of Screencasts
Screencasts = contentRepository
.Get(c => c.ContentTypeID == 1);
//Retrieve a list of Podcasts
Podcasts = contentRepository
.Get(c => c.ContentTypeID == 2);
//Retrieve a list of Documents
Documents = contentRepository
.Get(c => c.ContentTypeID == 3);
}
public MediaViewModel(int id)
{
this.dbcontext = new MyContext(System.Configuration.ConfigurationManager.ConnectionStrings["MyContext"].ConnectionString);
this.contentRepository = new Repository<Content>(dbcontext);
this.feedbackRepository = new Repository<Feedback>(dbcontext);
this.dbcontext.Configuration.LazyLoadingEnabled = true;
//Retrieve a list of Screencasts
Screencasts = contentRepository
.Get(c => c.ContentTypeID == 1);
//Retrieve a list of Podcasts
Podcasts = contentRepository
.Get(c => c.ContentTypeID == 2);
//Retrieve a list of Documents
Documents = contentRepository
.Get(c => c.ContentTypeID == 3);
//Retrieve selected screencast
SelectedMediaItem = contentRepository
.Get(c => c.ContentID == id)
.FirstOrDefault();
}
}
//获取
公共行动结果屏幕广播(int ID)
{
mvm=新的ViewModels.MediaViewModel(ID);
返回视图(mvm);
}
//职位
[HttpPost]
公共行动结果屏幕广播(MediaViewModel mvm)
{
反馈newFeedback=新反馈();
newFeedback.UserID=mvm.FeedbackComment.UserID;
newFeedback.ContentID=mvm.FeedbackComment.ContentID;
newFeedback.CourseID=null;
newFeedback.Timestamp=DateTime.Now;
newfeedbackstring=mvm.FeedbackComment.FeedbackString;
//初始化反馈存储库并保存更改
feedbackRepository=新存储库(dbcontext);
feedbackRepository.Add(newFeedback);
feedbackRepository.SaveChanges();
返回视图(mvm);
}
当我在URL/Media/Screencast/1处调用页面开始时,它会加载一个viewmodel,其中填充了所选媒体的详细信息,所有内容都会按预期显示
当我尝试发回此页面时,我的viewmodel详细信息将丢失,尽管注释实际上已按预期保存,但我需要访问另一个页面并重新访问,然后才能看到
如何将@Html.BeginFor中的视图模型作为附加参数传递
我的ViewModel如下所示:
@{
ViewBag.Title = "Screencast";
Layout = "~/Views/Shared/_Layout.cshtml";
}
@model Project.ViewModels.MediaViewModel
<body onload="initialize()">
<div class="container region3wrap_screencast">
<div class="row content_top_contact">
<div class="nine columns">
<ul class="breadcrumbs">
<li><a href="@Url.Action("Index","Home")">Home</a></li>
<li><a href="@Url.Action("Index","Media")">Media</a></li>
<li><a href="@Url.Action("Screencast","Media")">Screencast</a></li>
<li class="current"><a href="#">@Model.SelectedMediaItem.Name</a></li>
</ul>
</div>
</div>
</div>
<div class="twelve columns leave-comment">
<h3>Leave a Comment</h3>
@using(Html.BeginForm("Screencast","Media", FormMethod.Post, Model))
{
<div class="row">
<div class="six columns">
<div class="row">
<div class="six columns">
@Html.LabelFor(c => c.FeedbackComment.UserID)
@Html.TextBoxFor(c => c.FeedbackComment.UserID)
</div>
<div class="six columns">
@Html.LabelFor(c => c.FeedbackComment.ContentID)
@Html.TextBoxFor(c => c.FeedbackComment.ContentID)
</div>
<div class="row">
<div class="twelve columns">
@Html.LabelFor(c => c.FeedbackComment.FeedbackString)
@Html.TextAreaFor(c => c.FeedbackComment.FeedbackString)
</div>
</div>
</div>
</div>
</div>
<input type="submit" value="Submit button" class="medium button bottom20"/>
}
</div>
</body>
//GET
public ActionResult Screencast(int ID)
{
mvm = new ViewModels.MediaViewModel(ID);
return View(mvm);
}
//POST
[HttpPost]
public ActionResult Screencast(MediaViewModel mvm)
{
Feedback newFeedback= new Feedback();
newFeedback.UserID = mvm.FeedbackComment.UserID;
newFeedback.ContentID = mvm.FeedbackComment.ContentID;
newFeedback.CourseID = null;
newFeedback.Timestamp = DateTime.Now;
newFeedback.FeedbackString = mvm.FeedbackComment.FeedbackString;
//Initialize the Feedback Repository and save changes
feedbackRepository = new Repository<Feedback>(dbcontext);
feedbackRepository.Add(newFeedback);
feedbackRepository.SaveChanges();
return View(mvm);
}
public class MediaViewModel
{
private Repository<Content> contentRepository;
private Repository<Feedback> feedbackRepository;
private MetaLearningContext dbcontext;
public int screencastID { get; set; }
public IEnumerable<Content> Screencasts { get; set; }
public IEnumerable<Content> Podcasts { get; set; }
public IEnumerable<Content> Documents { get; set; }
public IEnumerable<Feedback> FeedbackComments { get; set; }
public Content SelectedMediaItem { get; set; }
public MetaLearningUser User { get; set; }
public MetaLearningUser FeedbackAuthor { get; set; }
public Feedback FeedbackComment { get; set; }
public MediaViewModel()
{
this.dbcontext = new MyContext(System.Configuration.ConfigurationManager.ConnectionStrings["MyContext"].ConnectionString);
this.contentRepository = new Repository<Content>(dbcontext);
this.feedbackRepository = new Repository<Feedback>(dbcontext);
this.dbcontext.Configuration.LazyLoadingEnabled = true;
//Retrieve a list of Screencasts
Screencasts = contentRepository
.Get(c => c.ContentTypeID == 1);
//Retrieve a list of Podcasts
Podcasts = contentRepository
.Get(c => c.ContentTypeID == 2);
//Retrieve a list of Documents
Documents = contentRepository
.Get(c => c.ContentTypeID == 3);
}
public MediaViewModel(int id)
{
this.dbcontext = new MyContext(System.Configuration.ConfigurationManager.ConnectionStrings["MyContext"].ConnectionString);
this.contentRepository = new Repository<Content>(dbcontext);
this.feedbackRepository = new Repository<Feedback>(dbcontext);
this.dbcontext.Configuration.LazyLoadingEnabled = true;
//Retrieve a list of Screencasts
Screencasts = contentRepository
.Get(c => c.ContentTypeID == 1);
//Retrieve a list of Podcasts
Podcasts = contentRepository
.Get(c => c.ContentTypeID == 2);
//Retrieve a list of Documents
Documents = contentRepository
.Get(c => c.ContentTypeID == 3);
//Retrieve selected screencast
SelectedMediaItem = contentRepository
.Get(c => c.ContentID == id)
.FirstOrDefault();
}
}
公共类MediaViewModel
{
私有存储库内容存储库;
私有存储库反馈存储库;
私有MetaLearningContext-dbcontext;
public int screencastID{get;set;}
公共IEnumerable屏幕广播{get;set;}
公共IEnumerable播客{get;set;}
公共IEnumerable文档{get;set;}
公共IEnumerable反馈注释{get;set;}
公共内容SelectedMediaItem{get;set;}
公共MetaLearningUser用户{get;set;}
公共MetaLearningUser反馈作者{get;set;}
公共反馈意见{get;set;}
公共媒体视图模型()
{
this.dbcontext=new MyContext(System.Configuration.ConfigurationManager.ConnectionString[“MyContext”].ConnectionString);
this.contentRepository=新存储库(dbcontext);
this.feedbackRepository=新存储库(dbcontext);
this.dbcontext.Configuration.LazyLoadingEnabled=true;
//检索屏幕广播列表
Screencasts=contentRepository
.Get(c=>c.ContentTypeID==1);
//检索播客列表
Podcasts=contentRepository
.Get(c=>c.ContentTypeID==2);
//检索文档列表
文档=内容存储库
.Get(c=>c.ContentTypeID==3);
}
公共媒体视图模型(int-id)
{
this.dbcontext=new MyContext(System.Configuration.ConfigurationManager.ConnectionString[“MyContext”].ConnectionString);
this.contentRepository=新存储库(dbcontext);
this.feedbackRepository=新存储库(dbcontext);
this.dbcontext.Configuration.LazyLoadingEnabled=true;
//检索屏幕广播列表
Screencasts=contentRepository
.Get(c=>c.ContentTypeID==1);
//检索播客列表
Podcasts=contentRepository
.Get(c=>c.ContentTypeID==2);
//检索文档列表
文档=内容存储库
.Get(c=>c.ContentTypeID==3);
//检索选定的屏幕广播
SelectedMediaItem=contentRepository
.Get(c=>c.ContentID==id)
.FirstOrDefault();
}
}
这可能是因为SelectedMediaItem没有与表单中的其他值一起发布。因此,当表单序列化为模型时,SelectedMediaItem属性为null。
您可以做的只是在邮寄表单中添加以下内容:
@Html.HiddenFor(c => c.SelectedMediaItem)
这可能是因为SelectedMediaItem没有与表单中的其他值一起发布。因此,当表单序列化为模型时,SelectedMediaItem属性为null。 您可以做的只是在邮寄表单中添加以下内容:
@Html.HiddenFor(c => c.SelectedMediaItem)
只是想补充一下Mattias的答案: 1) 他是对的——如果您想要一些信息重定向到何处,请将其放置在隐藏字段中或将其作为参数添加到表单方法中 2) MVC中常见的模式是PRG(Post/Redirect/Get),因此本质上,您的帖子应该用RedirectToAction(“Screencast”)而不是视图来回答。() 要使用它,您应该稍微重构一下您的函数:
public ActionResult Screencast(int ID)
{
mvm = new ViewModels.MediaViewModel(ID); //The id is in the viewmodel i think
return View(mvm);
}
[HttpPost]
//Even if unused include the formcollection for cases where the GET has the same signature
public ActionResult Screencast(int ID, MediaViewModel mvm, FormCollection collection)
{
//Do something
return RedirectToAction("Screencast", new { id = ID });
}
在你看来:
@using(Html.BeginForm("Screencast", new { id = Model.SelectedMediaItem }))
{ ... }
- 可能建议的重载需要更多参数,但您将看到