2

Ok, here is my problem. I am trying to pass a view model of mine, which has multiple list in it, to my view. Then in my view i need to edit the different list. Then on my post i need to save the edits. Although, when i pass my viewmodel back to my post, it is empty! Can somebody explain what i am doing wrong?

Controller

    public ActionResult ManageNewsArticles()
    {
        NewsViewModel newsViewModel = new NewsViewModel();

        newsViewModel.ListBreakingNews = db.NewsArticles.Where(n => n.PageSetupID == 1).ToList<NewsArticle>();
        newsViewModel.ListMainArticle = db.NewsArticles.Where(n => n.PageSetupID == 2).ToList<NewsArticle>();
        newsViewModel.ListSubNews1 = db.NewsArticles.Where(n => n.PageSetupID == 3).ToList<NewsArticle>();
        newsViewModel.ListSubNews2 = db.NewsArticles.Where(n => n.PageSetupID == 4).ToList<NewsArticle>();
        newsViewModel.ListSubNews3 = db.NewsArticles.Where(n => n.PageSetupID == 5).ToList<NewsArticle>();

        return View(newsViewModel);
    }

    [HttpPost]
    public ActionResult ManageNewsArticles(NewsViewModel newsViewModel)
    {
        if (ModelState.IsValid)
        {
            db.SaveChanges();
            return RedirectToAction("Admin");
        }

        return View(newsViewModel);
    }

here is my View

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<TrueNews.ViewModels.NewsViewModel>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Manage News Articles
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <h2>Manage News Articles</h2>

    <% Html.EnableClientValidation(); %>

    <% using (Html.BeginForm(Model)) {%>
        <%: Html.ValidationSummary(true) %>

        <fieldset>
            <%: Html.EditorForModel(Model) %>
            <p>
                <input type="submit" value="Save" />
            </p>
        </fieldset>

    <% } %>

    <div>
        <%: Html.ActionLink("Back to Admin Controls", "Admin") %>
    </div>
</asp:Content>

NewsViewModel

public class NewsViewModel
    {
        public List<NewsArticle> ListBreakingNews { get; set; }
        public List<NewsArticle> ListMainArticle { get; set; }
        public List<NewsArticle> ListSubNews1 { get; set; }
        public List<NewsArticle> ListSubNews2 { get; set; }
        public List<NewsArticle> ListSubNews3 { get; set; }
    } // End of Class
3
  • We need the code for the viewmodel too
    – CrazyDart
    Dec 10, 2010 at 22:04
  • just added the view model, nothing special Dec 10, 2010 at 22:18
  • Try initializing the lists in the view model constructor.
    – Dmitry S.
    Dec 13, 2010 at 22:34

3 Answers 3

0

First of all, I assume that you use linq2sql or something similar.

In order to update an object in your database, that object has to be fetched through a DataContext.

Inside your method "ManageNewsArticles" you're calling db.SaveChanges(); but since there is no objects loaded through db no rows will get updated.

A solution to this is to fetch all news you want to update, and then use the Controller.UpdateModel method to update your actual instances, and then call db.SaveChanges(); to persist your changes.

2
  • I'm using the entity framework. And honestly i really didn't finish that part of the save process. I kinda just put that there for refrence. I didn't want to finish that until i was getting data back from my view. And with the Entity framework wouldn't that data still be loaded within the db object? Dec 10, 2010 at 22:10
  • Ah sorry, must be tired :) Please post your ViewModel definition.
    – alexn
    Dec 10, 2010 at 22:12
0

Try using the

UpdateModel(NewsViewModel);  
db.SaveChanges();
return RedirectToAction("Admin");
1
  • It should not be necessary since the view model is an action method parameter. But it might show you the errors.
    – Dmitry S.
    Dec 13, 2010 at 22:33
0

I've never tried using EditorFor on lists of complex objects. I'm guessing that MVC is unable to encode your NewsArticle objects in such a way that they can be reassembled into a NewsViewModel object. Have you tried using something like Firebug to see what the actual POST looks like? What are the query parameter keys and values?

You may be able to simply take an IEnumerable<NewsArticle>, and then re-parse that out using the same logic you use in the ManageNewsArticles method. Give that a shot, and let us know what you find out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.