D
D
Dmitry Filandor2018-07-01 16:49:36
ASP.NET
Dmitry Filandor, 2018-07-01 16:49:36

Why doesn't the model go from the view to the controller?

Hello!
asp mvc 6.
Everything seems to be correct, but for some reason, when submitting, null comes to the controller.

//открываю представление с списком моделей

 public ActionResult Banners()
        {
            List<BusinessLayer.Events.Banners> BannersMdl = new List<BusinessLayer.Events.Banners>();

BannersMdl = BusinesLogic.GetBanners();          

            return View(BannersMdl);
        }

the list of models goes to the view, everything is filled in:
@model IEnumerable<BusinessLayer.Events.Banners>


        @using (Html.BeginForm("Banners", "AD", FormMethod.Post))              
                {


                    <fieldset>

                       @foreach (var Ad in Model)
                       {
                           switch (Ad.TypeBanner)
                           {

                               case 1:
                                <h4>Большой главный AD1</h4>
                                   break;

                               case 2:
                                <h4>Событие сайдбар ПК AD2</h4>
                                   break;

                               case 3:
                                <h4>Событие сайдбар мобильный AD3</h4>
                                   break;

                               case 4:
                                <h4>Событие шапка ПК AD4</h4>
                                   break;

                               case 5:
                                <h4>Событие шапка мобильный AD5</h4>
                                   break;

                           }

                      


                        @Html.TextAreaFor(m => Ad.Html, new { @class = "Width500", @style = "height:130px;" })<br><br>

                       }

                     
                      
                       
                         


                        <center> <div style="font-size:large;" id="msg">  </div></center>
                        <p>
                            <input type="submit" id="getCoordsButton" style="float:left" value="Сохранить" />


                            <span id="sendingProgress" style="display: none;">
                                Сохранение...
                                <img src='@Href("~/Images/TEMPLATE/ANIMATIONS/load0.GIF")' />
                            </span>
                        </p>

                    </fieldset>


                            }

accept view with new data on submit
[HttpPost]
        [HttpPost]     
        public ActionResult Banners(List<BusinessLayer.Events.Banners> BannersMdl)
        {
.....

and here BannersMdl = null.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Nemiro, 2018-07-01
@LifeAct

Most likely, the names of the HTML elements are incorrect (the same). Try using a regular for , then the output should be names with an index (type: [0].Html , [1].Html , etc.):

@for (int i = 0, count = Model.Count; i < count; ++i)
{
  @Html.TextAreaFor(m => m[i].Html, new { @class = "Width500", @style = "height:130px;" })
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question