0

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.

3
  • There's quite similar type of question found here: stackoverflow.com/questions/18237945/… Commented Oct 10, 2017 at 12:16
  • @GertArnold let me add some more code here. Commented Oct 10, 2017 at 12:17
  • @GertArnold I have updated the question, Please have a look. Commented Oct 10, 2017 at 12:25

2 Answers 2

1

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)
Sign up to request clarification or add additional context in comments.

2 Comments

If you like, I have a post about this in my blog :) debuxing.com/model-binding-to-a-list-of-objects
Thanks a lot @Aleem glad that you like it and that it worked.
0

You need to replace @foreach loop on view

 @foreach (var field in Model.ContentFields)
                            {

By for loop

@for (var i = 0; i < Model.ContentFields.Count; i++)
            {

This will bind your list to model, while sending data to controller

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.