MVC 5 - 在表单(视图)上输入的值未通过控制器

时间:2016-11-08 17:55:30

标签: asp.net-mvc

我为表单提交创建了一个View,我希望在单击Submit按钮后将表单数据传递给Controller。

为了简单起见,我缩小了模型和视图。

当我在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的新手,任何帮助都表示赞赏。提前谢谢!

1 个答案:

答案 0 :(得分:2)

编辑:问题似乎出现在您的ReviewViewModel中。我前面没有MVC项目,但我认为你可能会错过你对象的getter和setter。你可以试试像:

public class ReviewViewModel
    {
        public VideoTypeModel VideoTypeModel {get; set; }
        public VideoModel VideoModel {get; set; }
        public ReviewModel ReviewModel {get; set; }
    }

在控制器中,您需要添加Post标签。通常,您将为HttpGet执行一个操作,对HttpPost执行一个

操作
public ActionResult SubmitReview()
{
    return View();
}

[HttpPost]
public ActionResult SubmitReview(ReviewViewModel reviewViewModel)
{
    ReviewViewModel reviewViewModelx = reviewViewModel;
    SendReviewEmail ce = new SendReviewEmail(null);
    SendReviewEmailToTech ste = new SendReviewEmailToTech(null);
    return View(reviewViewModel);
}