0

我正在尝试让 HTTPPost 为视频页面的评论部分工作,但在回发时遇到了一些困难。保存正在记录到数据库中,但在重新加载页面时,我无法找到第一次找到的 @Model.SelectedMediaItem.Name 变量。我的html如下:

@{
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 /Media/Screencast/1 开始调用页面时,它会加载一个视图模型,其中填充了 SelectedMediaItem 的详细信息,并且所有内容都按预期显示。

当我尝试发回此内容时,尽管评论实际上已按预期保存,但我的视图模型详细信息正在丢失,我需要访问另一个页面并重新访问,然后才能看到。

如何在 @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();              

    }

}
4

2 回答 2

3

这可能是因为 SelectedMediaItem 没有与表单中的其他值一起发布。因此,当表单被序列化为模型时,SelectedMediaItem 属性为空。你可以做的就是在发布表单中添加这个:

@Html.HiddenFor(c => c.SelectedMediaItem)
于 2013-07-04T16:42:31.973 回答
3

只是为了从 Mattias 的答案中添加一些内容:

1)他是对的 - 如果您想要一些重定向信息,请将其放在隐藏字段中或将其作为参数添加到 Form-Method。

2) MVC 中常见的模式是 PRG (Post/Redirect/Get),所以基本上你的 POST 应该用 RedirectToAction("Screencast") 而不是视图来回答。(http://en.wikipedia.org/wiki/Post/Redirect/Get

要使用它,您应该稍微重构您的函数:

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 })) 
{ ... }
  • 可能是建议的重载需要更多参数,但您会看到...
于 2013-07-04T23:28:52.257 回答