开发者

Passing ViewModel from View to Controller

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>
        </fiel开发者_JAVA技巧dset>

    <% } %>

    <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


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.


Try using the

UpdateModel(NewsViewModel);  
db.SaveChanges();
return RedirectToAction("Admin");


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜