How to update multiple models at the same time in ASP.NET MVC 5 Entity Framework

asp.net-mvc entity-framework-6

Question

I am trying to update Two models at the same time.

Models:

  1. Page
  2. Fields

One page has multiple fields, I want them to update at the same time.

public class PageEditViewModel
{
    public Page mPage { get; set; }

    public IEnumerable<Field> Fields { get; set; }
}

Here is my View:

<div class="row">
                            <div class="col-md-12">
                                <h3>Fields</h3>
                                @foreach (var field in Model.ContentFields)
                                {
                                    @Html.HiddenFor(m => field.Id)

                                    switch (field.FieldType)
                                    {
                                        case "TextBox":

                                            <div class="form-group">
                                                <label class="control-label">
                                                     @field.FieldName<span class="required"> * </span>

                                                </label>
                                                @Html.TextBoxFor(m => field.Content , new { @class = "form-control" })
                                            </div>
                                            break;

                                        case "TextArea":
                                            <div class="form-group">
                                                <label class="control-label">
                                                    @field.FieldName<span class="required"> * </span>
                                                </label>
                                                @Html.TextAreaFor(m => field.Content, new { @class = "form-control" })
                                            </div>
                                            break;

                                        case "Image":
                                            <div class="form-group">
                                                <label class="control-label">
                                                    @field.FieldName<span class="required"> * </span>
                                                </label>
                                                <input type="file" name="contentImage" id="cImage" class="form-control" accept="image/*" />
                                            </div>
                                            break;
                                    }
                                }
                            </div>

                        </div>

And Controller:

public ActionResult Update(PageEditViewModel viewModel)
{
    if (!ModelState.IsValid)
    {
        var page = _context.MenuPages.Single(s => s.Id == viewModel.mPage.Id);
        var contentFields = _context.ContentFields.Where(c => c.MenuPageId == page.Id);

        var viewM = new PageEditViewModel
        {
            DashboardHeading = "Edit a Page",
            mPage = page,
            ContentFields = contentFields
        };
        return View("EditPage", viewM);
    }


    var pageEdit = _context.MenuPages.SingleOrDefault(p => p.Id == viewModel.mPage.Id);

    pageEdit.Name = viewModel.mPage.Name;
    pageEdit.IsActive = viewModel.mPage.IsActive;
    pageEdit.IsShowInMenu = viewModel.mPage.IsShowInMenu;

    //            _context.SaveChanges();

    foreach (var field in viewModel.ContentFields)
    {
        var cfield = _context.ContentFields.SingleOrDefault(f => f.Id == field.Id);

        cfield.Content = field.Content;

    }
    _context.SaveChanges();

When I Send the data from View to Controller, I get the data for Pages but Null for the Fields Model (Object Reference not set to an instance...).

I am looking forward to any guide from members here.

Thanks.

1
0
10/10/2017 12:25:07 PM

Popular Answer

I think Umesh answer is correct. After changing your loop, how are you setting your Html.TextBoxFor, HiddenFor and so on?

It should be:

@Html.HiddenFor(m => m.ContentFields[i].Id)
1
10/10/2017 1:01:12 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow