deSex
deSex

Reputation: 33

Consecutive AJAX Posts C# MVC

I'm trying to post consecutively through AJAX, but it doesn't post the second time around, I guess that's because the page doesn't reload. The post is from different cells in a table and goes to different ActionResults and everything is located within a MVC section where I render everything.

I'm working in MVC and this is my view where the posts takes place.

First post:

<tr>
@using (Ajax.BeginForm("ChangeContactInformation", null, new AjaxOptions { UpdateTargetId = "profileTable" }, FormMethod.Post))
{
  <td>
      @Html.TextBoxFor(x => x.CurrentUser.ContactInformation, new { Name = "ContactInformation" })<button type="submit">Spara</button>
  </td>
}
</tr>

Second post:

<tr>
@using (Ajax.BeginForm("ChangeTemporaryMessage", null, new AjaxOptions { UpdateTargetId = "profileTable" }, FormMethod.Post))
{
  <td>
      @Html.TextBoxFor(x => x.CurrentUser.ContactInformation, new { Name = "TemporaryMessage" })<button type="submit">Spara</button>
  </td>
}
</tr>

Here's the controller:

        [HttpPost]
    public ActionResult ChangeTemporaryMessage(string TemporaryMessage)
    {
        var model = (ProfileBlockViewModel)Session["model"];
        ConnectionHelper.ChangeTemporaryMessage(TemporaryMessage, model.CurrentUser.UserID);
        return RedirectToAction("Index", model);
    }

    [HttpPost]
    public ActionResult ChangeContactInformation(string ContactInformation)
    {
        var model = (ProfileBlockViewModel)Session["model"];
        ConnectionHelper.ChangeContactInformation(ContactInformation, model.CurrentUser.UserID);
        return RedirectToAction("Index", model);
    }

So the first one works perfectly, but the problem is that after that post is finished and the data is updated, I cannot post from the second one or post again from the first one - no consecutive posts allowed, which is exactly what I want.
Is there anyway I can achieve this?

Thanks in advance!
deSex

Upvotes: 0

Views: 218

Answers (1)

deSex
deSex

Reputation: 33

Sigh... got it working by making the following changes:

I changed this in the view

    <tr>
        @using (Ajax.BeginForm("ChangeTemporaryMessage", null, new AjaxOptions { UpdateTargetId = "profileTable" }, FormMethod.Post))
        {
            <td>
                @Html.TextBoxFor(x => x.CurrentUser.ContactInformation, new { Name = "TemporaryMessage" })<button type="submit">Spara</button>
            </td>
        }
    </tr>

To this:

    <td>
    @using (Ajax.BeginForm("ChangeTemporaryMessage", null, new AjaxOptions { UpdateTargetId = "profileTable" }, FormMethod.Post))
    {
        @Html.TextBoxFor(x => x.CurrentUser.TemporaryMessage, new { Name = "TemporaryMessage" })<button type="submit">Spara</button>
    }
    </td>

I solved it by putting the @using directive inside the < td > tag, which was around it before.

Not sure WHY this is enabling consecutive posts, so if you know feel free to educate me! :)

Regards
deSex

Upvotes: 1

Related Questions