powered by simpleCommunicator - 2.0.59     © 2026 Programmizd 02
Целевая тема:
Создать новую тему:
Автор:
Закрыть
Цитировать
Форумы / ASP.NET [игнор отключен] [закрыт для гостей] / ASP.NET MVC. Проблема с POST запросом и Html.RenderAction.
2 сообщений из 2, страница 1 из 1
ASP.NET MVC. Проблема с POST запросом и Html.RenderAction.
    #38627495
wizzzi
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
Привет всем. Пишу, что то на подобие Twitter. Столкнулся с такой проблемой, есть страница пользователя на которой размещена информация о нем и его сообщения. С помощью Html.RenderAction вставляю на нее форму для отправки сообщений(кнопка+поле ввода). При отправки сообщения оно добавляется к списку уже имеющихся и дальше если обновить страницу, опять выполняется этот запрос(на добавление сообщения). Вот код:

Контроллер:
Код: c#
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
      [Authorize]
        public ActionResult UserPage(int id)
        {
            ViewBag.ID = _repoUser.Users.Where(x => x.Email == User.Identity.Name).Select(x => x.ID).First();
            UserPageViewModel userPage = new UserPageViewModel { user = _repoUser.Users.Where(x => x.ID == id).First(), listMessage = _repoMsg.Messages.Where(x => x.Addressee == id) };
            return View(userPage);
        }

        [Authorize]
        [HttpPost]
        public ActionResult UserPage(int id, string action, UserPageViewModel message)
        {
            ViewBag.ID = _repoUser.Users.Where(x => x.Email == User.Identity.Name).Select(x => x.ID).First();
            UserPageViewModel userPage = new UserPageViewModel { user = _repoUser.Users.Where(x => x.ID == id).First(), listMessage = _repoMsg.Messages.Where(x => x.Addressee == id) };
            return View(userPage);
        }

        [Authorize]
        public ActionResult SendMessage()
        {
            return View();
        }

        [Authorize]
        [HttpPost]
        public ActionResult SendMessage(int id, string action, Message message)
        {
            if (ModelState.IsValid && action == "sendMsg")
            {
                message.Addressee = id;
                message.Sender = _repoUser.Users.Where(x => x.Email == User.Identity.Name).Select(x => x.ID).First();
                message.TimeMsg = DateTime.Now;

                _repoMsg.Add(message);
            }
            return View();
        }



Представление:
Код: plaintext
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
@model MyTwitter.Models.UserPageViewModel

@{
    ViewBag.Title = "UserPage";
}

<h2>UserPage</h2>

@{Html.RenderAction("UserPage", "SendMessage");}

@using (Html.BeginForm())
{
    <div>
        <p>@Model.user.ID</p>
        <p>@Model.user.Email</p>
        <p>@Model.user.UserName</p>
        <p>@Model.user.Password</p>
        <p>@User.Identity.Name</p>
        @{if ((int)@ViewBag.ID == Model.user.ID)
          {  @Html.ActionLink("Изменить профиль", "Edit", "EditUserPage", new { id = Model.user.ID }, null) };
        }
        @Html.ActionLink("Выход", "SignOut", "Auth")
    </div>
    
    <div>
        @foreach (var i in Model.listMessage)
        {
            <div>
                <fieldset>
                    <legend>@i.Sender, @i.TimeMsg</legend>
                    <p>@i.Msg</p>
                </fieldset>
            </div>
        }
    </div>
}



Представление для отправки сообщений:

Код: plaintext
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
@model MyTwitter.Models.Message

@{
    ViewBag.Title = "SendMessage";
}

<h2>SendMessage</h2>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@using (Html.BeginForm())
{
    <div>
        <fieldset>
            <legend>Account Information</legend>

            <div class="editor-label">
                @Html.LabelFor(m => m.Msg)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(m => m.Msg)
                @Html.ValidationMessageFor(m => m.Msg)
            </div>
            <p>
                <button type="submit" name="action" value="sendMsg">Отправить</button>
            </p>
        </fieldset>
    </div>
}


Что можно сделать, кроме как сделать для этого одну вьюху.
...
Рейтинг: 0 / 0
ASP.NET MVC. Проблема с POST запросом и Html.RenderAction.
    #38627701
wizzzi
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
Вроде бы решил проблему добавлением во View такой вещи: @using (Html.BeginForm("SendMessage", "UserPage")).
...
Рейтинг: 0 / 0
2 сообщений из 2, страница 1 из 1
Форумы / ASP.NET [игнор отключен] [закрыт для гостей] / ASP.NET MVC. Проблема с POST запросом и Html.RenderAction.
Найденые пользователи ...
Разблокировать пользователей ...
Читали форум (0):
Пользователи онлайн (0):
x
x
Закрыть


Просмотр
0 / 0
Close
Debug Console [Select Text]