MVC3 - Why are navigation properties invalid for postback?

I cannot understand how the navigation properties for my model (EF4) will not be empty after posting. I have the same problem on several main pages.

i.e. I have a Rate table with the LoginID foreign key (from the login table).

In my HTTPGet editing method, I find the specific rating I want:

Estimate estimate = db.Estimates.Find(id)

From here I can access the login properties and any properties of this entry from the login table.

I pass this estimate as a model for my strongly typed view. When my HTTPPost Edit method is called (and the model is passed to it as a parameter), my login navigation property is null, and I cannot access it. I can get around this, of course, because I have a LoginID field in my evaluation class, but it feels very awkward and as if I overlook the key advantage of an entity structure.

I have the same problem on multiple pages where I pass a model with a navigation property to my view and the view returns a model with a null navigation property.

Below is an example of the code I encountered with the problem:

    [HttpPost]
    public ActionResult Edit(Estimate estimate)
    {

        var test = estimate.Login.CompanyProfileID;
        ...

Model.Login, , , . , .

+5
4

, , MVC , " ", , POST .

, POST, POST , , null.

, Login, .

@Html.HiddenFor(m => m.Login.ID)
@Html.HiddenFor(m => m.Login.Name)

, , .. . , , .

+6

MVC viewstate . , POST, . LoginID.

@Html.HiddenFor(m => m.LoginID)
0

, . post action , , , , , (, ) , . , , . db, , (, ).

:

 [Authorize]
        public ActionResult Edit(int id)
        {
            var movie = movieService.GetMovieById(id);
            if (new UserService().Current().UserId == movie.UserID)
            {
                EditMovieModel model = new EditMovieModel(id);
                return View(model);
            }
            else
            {
                throw new System.Web.HttpException(403, "403 Forbidden");
            }
        }

        [HttpPost]
        [Authorize]
        public ActionResult Edit(Movie movie)
        {
            var realMovie = movieService.GetMovieById(movie.ID);
            if (new UserService().Current().UserId == realMovie.UserID)
            {
                realMovie.Text = HtmlSanitizer.sanitize(movie.Plot);
                realMovie.CategoryID = movie.CategoryID;
                movieService.Update(realMovie);
            }
            else
            {
                throw new System.Web.HttpException(403, "403 Forbidden");
            }
            return RedirectToAction("Movie", new { id = realMovie.ID, title = realMovie.Permalink });
        }
0

The Estimate object that you return is limited to the viewport (along with any Bind attributes). Either add hidden fields for the properties you want, or reload the object from the context.

Note (the whole reason I am posting this answer to a long dead question) is that for EF6, if you reload the object from the context after .SaveChanges to partially EntityState.Modified (because you were smart), it will provide you CACHED and incomplete version of the object. You should do context.Entity (evaluation) .Reload () instead of .Find or .Where

0
source

All Articles