DanO
DanO

Reputation: 417

MVC 5 - values entered on form (View) not coming through to the Controller

I created a View for form submission and I expect the form data to be passed to the Controller once the Submit button clicked.

I scaled down the Models and views a little bit for simplicity.

When I Break on the HomeController and execute the step: ReviewViewModel reviewViewModelx = reviewViewModel; No values are filled in from the form that I would expect.

View:

@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 This view model contains 3 different data models, I am only showing the Review Class below as that is what is being populated.

    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;
            }
        }
    }

Review Data Model:

ReviewModel

    public class ReviewModel
{

    [Required]
    [Display(Name="Name: ")]
    public string Name { get; set; }

    [Required]
    [Display(Name="Degree:")]
    public string Degree { get; set; }

}

HomeController
As mentioned earlier, after first step, there is no values in the reviewViewModelx.ReviewModel.Name or reviewViewModelx.ReviewModel.Email (or any other fields that are in the model as I removed in this sample for brevity.

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


I'm somewhat of a newbie to MVC, any assistance is appreciated. Thanks in advance!

Upvotes: 2

Views: 835

Answers (1)

Daniel Congrove
Daniel Congrove

Reputation: 3679

Edit: It appears the issue may be in your ReviewViewModel. I don't have an MVC project in front me, but I think you may be missing your getters and setters for the objects. Can you try something like:

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

In your controller you need to add the Post tag. Typically you will have one action for the HttpGet and on for the 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);
}

Upvotes: 2

Related Questions