我試圖讓HTTPPost在視頻頁面的評論部分工作,但在回發時遇到了一些困難。保存被記錄到數據庫,但在重新加載頁面時,我無法找到第一次找到的@ Model.SelectedMediaItem.Name變量。我的HTML如下:如何將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);
}
當我調用頁面的URL與開始/媒體/截屏/ 1,它加載與填充的視圖模型其中包含SelectedMediaItem的詳細信息,並按預期顯示。
當我試圖回發到這我的viewmodel細節正在丟失,儘管評論實際上保存如期,我需要訪問另一個頁面,並在我能夠看到之前重訪。
如何將我的視圖模型作爲附加參數傳遞給@ Html.BeginFor?
我的視圖模型如下:
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();
}
}
你的文章沒有回報 - w帽子呢? – TGlatzer
對不起,我省略了控制器操作的底部。它保存了dbcontext,然後返回視圖 – Jay
我第一次導航到../Media/Screencast/1我通過調用「@ Url.Action(」Screencast「,」Media「,new {id = @mediaItem .ContentID})「。當我正在做的帖子,我已經在選定的媒體項目,即screecast1,但ViewModel中的值不會保留在回發到同一頁面,雖然值保存我收到模式屬性空引用 – Jay