简体   繁体   中英

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() { }
    }
}

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)

你必须试试这个

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM