0
votes

Good news everyone. I have a little problem. I can't show the data in table. I moved code to HomeContoller and it's fixed bug with null object.

Home controller

using NarkomApp.Models;

using System.Collections.Generic; using System.Web.Mvc;

namespace NarkomApp.Controllers { public class HomeController : Controller { NarkomEntities dbModel = new NarkomEntities();

    public ActionResult Index()
    {
        return View();
    }

    public ActionResult PersonalPage()
    {
        PersonalPageModel personalPageModel = new PersonalPageModel();

        List<USERS> list_Users = new List<USERS>();
        List<PERSONAL_INF> list_PersonalInf = new List<PERSONAL_INF>();

        personalPageModel.list_Users = list_Users;
        personalPageModel.list_PersonalInf = list_PersonalInf;

        personalPageModel.ChangePasswordErrorMessage = "Новый пароль совпадает со старым!";

        return View("~/Views/Home/PersonalPage.cshtml", personalPageModel);
    }

    public ActionResult AddOrEdit()
    {
        return View();
    }

    public ActionResult LogOut()
    {
        int iIdUser = (int)Session["iIdUsers"];
        Session.Abandon();
        return RedirectToAction("Index", "Home");
    }

}

View code:

@model NarkomApp.Models.PersonalPageModel


@if (Model != null)
        {
            <table border="0">

                <tr>
                    <td colspan="2"><h2>Персональные данные</h2></td>
                    <td></td>
                </tr>

                @foreach (var personalInf in Model.list_PersonalInf)
                {
                    <tr>
                        <td><p>Город:</p></td>
                        <td>@Html.Label(@personalInf.vCity, new { htmlAttributes = new { @class = "form-control" } })</td>
                    </tr>
                    <tr>
                        <td><p>ФИО:</p></td>
                        <td>@Html.Label(@personalInf.vFIO, new { htmlAttributes = new { @class = "form-control" } })</td>
                    </tr>
                    <tr>
                        <td><p>Индекс:</p></td>
                        <td>@Html.Label(@personalInf.vIndex, new { htmlAttributes = new { @class = "form-control" } })</td>
                    </tr>
                    <tr>
                        <td><p>Населенный пункт:</p></td>
                        <td>@Html.Label(personalInf.vLocality, new { htmlAttributes = new { @class = "form-control" } })</td>
                    </tr>
                    <tr>
                        <td><p>Улица:</p></td>
                        <
                        <td>@Html.Label(@personalInf.vStreet, new { htmlAttributes = new { @class = "form-control" } })</td>
                    </tr>
                    <tr>
                        <td><p>Дом/корпус/строение:</p></td>
                        <td>@Html.Label(@personalInf.vHome, new { htmlAttributes = new { @class = "form-control" } })</td>
                    </tr>
                    <tr>
                        <td><p>Квартира:</p></td>
                        <
                        <td>@Html.Label(@personalInf.vApartment, new { htmlAttributes = new { @class = "form-control" } })</td>
                    </tr>
                    <tr>
                        <td><p>Телефон:</p></td>
                        <td>@Html.Label(@personalInf.vPhone, new { htmlAttributes = new { @class = "form-control" } })</td>
                    </tr>
                }

                <tr>
                    <td colspan="2"><hr /></td>
                    <td></td>
                </tr>

                <tr>
                    <td colspan="2"><h2>Форма изменения данных</h2></td>
                    <td></td>
                </tr>

                @foreach (var personalInf in Model.list_PersonalInf)
                {
                    <tr>
                        <td><p>Город:</p></td>
                        <td>@Html.Editor(@personalInf.vCity, new { htmlAttributes = new { @class = "form-control" } })</td>
                    </tr>
                    <tr>
                        <td><p>ФИО:</p></td>
                        <td>@Html.Editor(@personalInf.vFIO, new { htmlAttributes = new { @class = "form-control" } })</td>
                    </tr>
                    <tr>
                        <td><p>Индекс:</p></td>
                        <td>@Html.Editor(@personalInf.vIndex, new { htmlAttributes = new { @class = "form-control" } })</td>
                    </tr>
                    <tr>
                        <td><p>Населенный пункт:</p></td>
                        <td>@Html.Editor(personalInf.vLocality, new { htmlAttributes = new { @class = "form-control" } })</td>
                    </tr>
                    <tr>
                        <td><p>Улица:</p></td>
                        <
                        <td>@Html.Editor(@personalInf.vStreet, new { htmlAttributes = new { @class = "form-control" } })</td>
                    </tr>
                    <tr>
                        <td><p>Дом/корпус/строение:</p></td>
                        <td>@Html.Editor(@personalInf.vHome, new { htmlAttributes = new { @class = "form-control" } })</td>
                    </tr>
                    <tr>
                        <td><p>Квартира:</p></td>
                        <
                        <td>@Html.Editor(@personalInf.vApartment, new { htmlAttributes = new { @class = "form-control" } })</td>
                    </tr>
                    <tr>
                        <td><p>Телефон:</p></td>
                        <td>@Html.Editor(@personalInf.vPhone, new { htmlAttributes = new { @class = "form-control" } })</td>
                    </tr>


                    <tr>
                        <td><input type="submit" value="Изменить" class="btn-submit" /></td>
                        <td><input type="reset" value="Очистка" class="btn-submit" /></td>
                    </tr>

                }


                <tr>
                    <td colspan="2"><hr /></td>
                    <td></td>
                </tr>

                <tr>
                    <td colspan="2"><h2>Форма изменения пароля</h2></td>
                    <td></td>
                </tr>

                @foreach (var users in Model.list_Users)
                {
                    <tr>
                        <td><p>Старый пароль:</p></td>
                        <td>
                            @Html.Editor(Model.NewPassword, new { htmlAttributes = new { @class = "form-control" } })<br />
                            @Html.ValidationMessage(Model.NewPassword, "", new { @class = "text-danger" })
                        </td>
                    </tr>

                    <tr>
                        <td><p>Новый пароль:</p></td>
                        <td>
                            @Html.Editor(@users.vPassword, new { htmlAttributes = new { @class = "form-control" } })<br />
                            @Html.ValidationMessage(@users.vPassword, "", new { @class = "text-danger" })
                        </td>
                    </tr>

                    <tr>
                        <td><p>Подтвердите пароль:</p></td>
                        <td>
                            @Html.Editor(@users.ConfirmPassword, new { htmlAttributes = new { @class = "form-control" } })<br />
                            @Html.ValidationMessage(@users.ConfirmPassword, "", new { @class = "text-danger" })
                        </td>
                    </tr>
                }


                <tr>
                    <td><input type="submit" value="Изменить" class="btn-submit" /></td>
                    <td><input type="reset" value="Очистка" class="btn-submit" /></td>
                </tr>

                <tr>
                    <td colspan="2"><hr /></td>
                    <td></td>
                </tr>

                <tr>
                    <td colspan="2"><p>Подписаться на рассылку</p> @Html.CheckBox("cbIsSendInf", true)</td>
                    <td></td>
                </tr>

                <tr>
                    <td colspan="2"><hr /></td>
                    <td></td>
                </tr>

                <tr>
                    <td colspan="2">@Html.ActionLink("Выход из аккаунта!", "LogOut", "Home", null, new { @class = "form-control" })</td>
                    <td></td>
                </tr>

            </table>

        } @* End if *@
        else
        {
            <h2>Проблема с сессией, пустой объект!</h2>
        }
3
It would help to see your view modelmaccettura
It's not entirely clear what you're doing here, but it looks like you're creating your view model on the controller itself. Don't do that. Create an instance in the action method. Controllers are instantiated and disposed with each request, so if you're somehow thinking you can share this instance between actions, you're incorrect.Chris Pratt

3 Answers

0
votes

Try and put PersonalPageModel personalPageModel = new PersonalPageModel(); inside your ActionResult or if you want it outside then try specifying public or private for your model. I hope that helps!

P.S. Run in Debug mode and put a breakpoint just before you return. Inspect your variables and see what is being held in them, I hope this helps you out to narrow down your problem!

0
votes

I think its better to return just a new PersonalPageModel than just editing it every action. Keep in mind that every one controller in any web application are stateless - so you will not have the same object in another request (ofc you can have static object, but I think it isnt good to use static model in this situation)

I think this should solve your situation:

  return View("~/Views/Home/PersonalPage.cshtml", new PersonalPageModel(){ 
            ChangePasswordErrorMessage = "Новый пароль совпадает со старым!",
            list_Users  = new List<USERS>(),
            list_PersonalInf  = new List<PERSONAL_INF>()
  });

Right now in your case in every request you create a new object of PersonalPageModel which isnt good I think.

0
votes

Create an instance of your View Model in the Action Method rather like below:

//PersonalPageModel personalPageModel = new PersonalPageModel();---> remove this

    // GET: PersonalPage
    public ActionResult PersonalPage()
    {
        PersonalPageModel personalPageModel = new PersonalPageModel(); // use here in the action method instead
        List<USERS> list_Users = new List<USERS>();
        List<PERSONAL_INF> list_PersonalInf = new List<PERSONAL_INF>();

        personalPageModel.list_Users = list_Users;
        personalPageModel.list_PersonalInf = list_PersonalInf;

        personalPageModel.ChangePasswordErrorMessage = "Новый пароль совпадает со старым!";

        return View("~/Views/Home/PersonalPage.cshtml", personalPageModel);
    }

Don't instantiate it in the Controller like you were doing before, it can expose it unplanned modifications and it's not the best as Chris Pratt pointed out.