フォームの送信用にビューを作成しました。送信ボタンをクリックすると、フォームデータがコントローラに渡されることが期待されます。MVC 5 - フォームに入力された値(ビュー)がコントローラに届かない
簡単にするため、モデルとビューのサイズを少し小さくしました。
HomeControllerを壊してステップを実行するとき: ReviewViewModel reviewViewModelx = reviewViewModel; 期待していたフォームから値が入力されていません。
ビュー:
@model ReviewViewModel
<div class="panel-body">
@using (Html.BeginForm("SubmitReview", "Home", FormMethod.Post, new { model = Model }))
{
@*@Html.AntiForgeryToken()*@
@Html.ValidationSummary("", new { @class = "text-danger" })
<div class="form-horizontal">
<div class="form-group">
@Html.LabelForRequired(model => model.ReviewModel.Name, htmlAttributes: new { @class = "control-label col-md-5" })
<div class="col-md-5">
@Html.EditorFor(model => model.ReviewModel.Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ReviewModel.Name, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelForRequired(model => model.ReviewModel.Degree, htmlAttributes: new { @class = "control-label col-md-5" })
<div class="col-md-5">
@Html.EditorFor(model => model.ReviewModel.Degree, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ReviewModel.Degree, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-5">
<input type="submit" value="Submit Review" class="btn btn-default" />
</div>
</div>
</div>
</div>
}
ReviewViewModelこのビューモデルは、それが取り込まれているものであると私は下のレビュー・クラスを示しています、3種類のデータモデルが含まれています。
public class ReviewViewModel
{
public VideoTypeModel VideoTypeModel;
public VideoModel VideoModel;
public ReviewModel ReviewModel;
public ReviewViewModel()
{
VideoTypeModel = new VideoTypeModel();
VideoModel = new VideoModel();
ReviewModel = new ReviewModel();
}
public ReviewViewModel(VideoTypeModel videoTypeModel, VideoModel videoModel, ReviewModel reviewModel)
{
this.VideoTypeModel = videoTypeModel;
this.VideoModel = videoModel;
if (reviewModel != null)
{
this.ReviewModel = reviewModel;
}
}
}
レビューデータモデル:
ReviewModel
public class ReviewModel
{
[Required]
[Display(Name="Name: ")]
public string Name { get; set; }
[Required]
[Display(Name="Degree:")]
public string Degree { get; set; }
}
にHomeController
先に述べたように、最初のステップの後、reviewViewModelx.ReviewModel.Nameには値が存在しませんか、 reviewViewModelx.ReviewModel.Email(またはモデルに含まれるその他のフィールドこのサンプルでは簡潔にするために削除されています。
[HttpPost]
public ActionResult SubmitReview(ReviewViewModel reviewViewModel)
{
ReviewViewModel reviewViewModelx = reviewViewModel;
SendReviewEmail ce = new SendReviewEmail(null);
SendReviewEmailToTech ste = new SendReviewEmailToTech(null);
return View(reviewViewModel);
}
私はどのような援助が高く評価され、ややMVCへの初心者のです。前もって感謝します!
BeginForm宣言でモデルを渡す必要はありません。実際、FormMethod.Postの後のパラメータはRouteValuesではなくHtmlAttributesでなければならないと思います。 apiを確認してください。あなたのモデルにIEnumerable型を持っていますか? – JanneP