user2206127
user2206127

Reputation: 47

MVC 3 Passing null from View to Controller

Whenever I try to pass a value from the view to the controller using a model, it shows up in the controller as null. I tried something very similar in another part of the project and did not have this problem. Why is this coming back null?

Here is the code from the controller:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MovieProject2.Models;

namespace MovieProject2.Controllers
{

public class ReviewsController : Controller
{
public ActionResult Edit(int id = -1)
{
    if (id < 0)
    return HttpNotFound();
    MovieReview review = MovieADO.getReviewByID(id);
    return View(review);
}

[HttpPost]
public ActionResult Edit(MovieReview review)
{
    if (review == null) return HttpNotFound();
    return View(review);
}
}

View:

@model MovieProject2.Models.MovieReview

@{
    ViewBag.Title = "Edit Review";
}

@{  //Not null here
    if(@Model != null) {<h2>Edit Review for @Model.MovieReviewed.Title</h2>
    <h4>Reviewed by @Model.Reviewer.Username</h4>}
                                                 else{<h2>Not Found</h2>
}
}

@using (Html.BeginForm())
{
    Html.ValidationSummary(true);
    <fieldset>
        <div class="editor-label">
            @Html.LabelFor(itemModel => Model.Rating)
        </div>
        <div class="editor-label">
            @Html.EditorFor(itemModel => Model.Rating)
            @Html.ValidationMessageFor(itemModel => Model.Rating)
        </div>
        <div class="editor-label">
            @Html.LabelFor(itemModel => Model.Review)
        </div>
        <div class="editor-label">
            @Html.EditorFor(itemModel => Model.Review)
            @Html.ValidationMessageFor(itemModel => Model.Review)
        </div>
        <p>
            <input type="submit" value="Change" />
        </p>
    </fieldset> 

}

Model:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MovieProject2.Models
{
    public class MovieReview
    {
        public int ReviewID { get; set; }
        public Movie MovieReviewed { get; set; }
        public User Reviewer { get; set; }
        public int Rating { get; set; }
        public string Review { get; set; }
        public DateTime DateReviewed { get; set; }

        public MovieReview() { }
    }
}

Upvotes: 2

Views: 3950

Answers (2)

Aleksandar Vucetic
Aleksandar Vucetic

Reputation: 14953

Instead of

[HttpPost]
public ActionResult Edit(MovieReview review)

write

[HttpPost]
public ActionResult Edit(MovieReview model)

(and rename it further in that method from review to model. It should work.

OR

rename property of MovieReview.Review to something else (for example, Review1). You cannot have the same name for a property and passed model object (case insensitive)

Upvotes: 6

Sagar Hirapara
Sagar Hirapara

Reputation: 1697

You have to try this

@using (Html.BeginForm("Action methodName","controllerName"))

Upvotes: 0

Related Questions