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 に追加パラメーターとして渡すにはどうすればよいですか?

私のViewModelは次のとおりです。

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 プロパティは null になります。あなたができることは、投稿フォーム内にこれを追加するだけです:

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