0

I have this strange behavior happening on my save method. Only part of the table is being saved and the rest is not saving. Seems like the model is not posting all the values or something else that I'm missing.

Model

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

namespace TotalDesk.Models
{
    public class ClientSMSAccountCustom
    {
        public ClientSMSAccountCustom()
        {
            this.Countries = new List<SelectListItem>();
        }

        public int CountryID { get; set; }
        public IList<SelectListItem> Countries { get; set; }


        public long ClientSMSAccountID { get; set; }
        public string CountryCode { get; set; }
        public string Username { get; set; }
        public string Password { get; set; }

        public int? Rem1LowAmount { get; set; }
        public string Rem1Email1 { get; set; }
        public string Rem1Email2 { get; set; }
        public string Rem1AdditionalEmail { get; set; }
        public bool Rem1SendToEmail1 { get; set; }
        public bool Rem1SendToEmail2 { get; set; }

        public int? Rem2LowAmount { get; set; }
        public string Rem2Email1 { get; set; }
        public string Rem2Email2 { get; set; }
        public string Rem2AdditioanlEmail { get; set; }
        public bool Rem2SendToEmail1 { get; set; }
        public bool Rem2SendToEmail2 { get; set; }

        public int? Rem3LowAmount { get; set; }
        public string Rem3Email1 { get; set; }
        public string Rem3Email2 { get; set; }
        public string Rem3AdditionalEmail { get; set; }
        public bool Rem3SendToEmail1 { get; set; }
        public bool Rem3SendToEmail2 { get; set; }
    }
}

View

@model TotalDesk.Models.ClientSMSAccountCustom
@{
    ViewBag.Title = "Business SMS";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

@Html.Partial("_side-bar-menu-business-sms")


<!-- Content Wrapper. Contains page content -->
  <div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
      <h1>
        Business SMS
        <small>Low Credit Alerts</small>
      </h1>
      <ol class="breadcrumb">
        <li><a href="~/business-sms/"><i class="fa fa-dashboard"></i> Dashboard</a></li>
      </ol>
    </section>


      <!-- Main content -->
    <section class="content">
      <!-- Small boxes (Stat box) -->
@using (Html.BeginForm("LowCreditAlert", "BusinessSMS", FormMethod.Post, new { enctype = "multipart/form-data" }))
        {

<div class="row">
        <div class="col-md-12">
<div class="box box-default">
              <div class="box-header with-border">
              <h3 class="box-title">Options</h3>

            </div>
            <!-- /.box-header -->
            <div class="box-body">
                <div class="row">
            <div class="col-md-3">
              <div class="form-group">
                <label>Country</label>
                @Html.DropDownListFor(model => model.CountryID, Model.Countries, new { @class = "form-control select2" })
              </div>
            </div>
            <!-- /.col -->
            <!-- /.col -->
            <div class="col-md-3">
              <!-- Date and time range -->
              <div class="form-group">
                <label>&nbsp;</label>

                <div class="input-group">
                  <button id="btnShowAlerts" name="submitButton" value="ShowAlerts" type="submit" class="btn btn-primary">Show Alerts</button>
                </div>
              </div>
              <!-- /.form group -->
            </div>
            <!-- /.col -->
          </div>
                <!-- /.row -->
            </div>

            <!-- /.box-body -->
          </div>
    </div>
</div>

      <div class="row">
        <div class="col-md-4">
          <!-- general form elements -->
          <div class="box box-primary">
            <div class="box-header with-border">
              <h3 class="box-title">Alert 1</h3>
            </div>
            <!-- /.box-header -->
            <!-- form start -->
            <form role="form">
              <div class="box-body">
                <div class="form-group">
                  <label for="Rem1LowAmount">Credit Amount</label><br />
                   @Html.TextBoxFor(m => m.Rem1LowAmount, new { @class = "form-control", style="width:100px;" })
                </div>
                <div class="form-group">
                  <label for="Rem1Email1">Email 1</label>
                  @Html.TextBoxFor(m => m.Rem1Email1, new { @class = "form-control", disabled = "disabled" })
                </div>
                <div class="checkbox">
                  <label>
                    @Html.EditorFor(m => m.Rem1SendToEmail1) Send to Email 1
                  </label>
                </div>
                <div class="form-group">
                  <label for="Rem1Email2">Email 2</label>
                  @Html.TextBoxFor(m => m.Rem1Email2, new { @class = "form-control", disabled = "disabled" })
                </div>
                <div class="checkbox">
                  <label>
                    @Html.EditorFor(m => m.Rem1SendToEmail2) Send to Email 2
                  </label>
                </div>
                <div class="form-group">
                  <label for="Rem1AdditionalEmail">Additional Email</label><br />
                  @Html.TextBoxFor(model => model.Rem1AdditionalEmail, new { @class = "form-control" })
                </div>
              </div>
              <!-- /.box-body -->

              <div class="box-footer">
                <button type="submit" id="btnSaveAlert1" name="submitButton" value="SaveAlert" class="btn btn-primary">Submit</button>
              </div>
            </form>
          </div>
          <!-- /.box -->
        </div>
        <!-- ./col -->

        <div class="col-md-4">
          <!-- general form elements -->
          <div class="box box-primary">
            <div class="box-header with-border">
              <h3 class="box-title">Alert 2</h3>
            </div>
            <!-- /.box-header -->
            <!-- form start -->
            <form role="form">
              <div class="box-body">
                <div class="form-group">
                  <label for="Rem2LowAmount">Credit Amount</label><br />
                   @Html.TextBoxFor(m => m.Rem2LowAmount, new { @class = "form-control", style="width:100px;" })
                </div>
                <div class="form-group">
                  <label for="Rem2Email1">Email 1</label>
                  @Html.TextBoxFor(m => m.Rem2Email1, new { @class = "form-control", disabled = "disabled" })
                </div>
                <div class="checkbox">
                  <label>
                    @Html.EditorFor(m => m.Rem2SendToEmail1) Send to Email 1
                  </label>
                </div>
                <div class="form-group">
                  <label for="Rem2Email2">Email 2</label>
                  @Html.TextBoxFor(m => m.Rem2Email2, new { @class = "form-control", disabled = "disabled" })
                </div>
                <div class="checkbox">
                  <label>
                    @Html.EditorFor(m => m.Rem2SendToEmail2) Send to Email 2
                  </label>
                </div>
                <div class="form-group">
                  <label for="Rem2AdditioanlEmail">Additional Email</label><br />
                  @Html.TextBoxFor(model => model.Rem2AdditioanlEmail, new { @class = "form-control" })
                </div>
              </div>
              <!-- /.box-body -->

              <div class="box-footer">
                <button type="submit" id="btnSaveAlert2" name="submitButton" value="SaveAlert" class="btn btn-primary">Submit</button>
              </div>
            </form>
          </div>
          <!-- /.box -->
        </div>
        <!-- ./col -->

        <div class="col-md-4">
          <!-- general form elements -->
          <div class="box box-primary">
            <div class="box-header with-border">
              <h3 class="box-title">Alert 3</h3>
            </div>
            <!-- /.box-header -->
            <!-- form start -->
            <form role="form">
              <div class="box-body">
                <div class="form-group">
                  <label for="Rem3LowAmount">Credit Amount</label><br />
                   @Html.TextBoxFor(m => m.Rem3LowAmount, new { @class = "form-control", style="width:100px;" })
                </div>
                <div class="form-group">
                  <label for="Rem3Email1">Email 1</label>
                  @Html.TextBoxFor(m => m.Rem3Email1, new { @class = "form-control", disabled = "disabled" })
                </div>
                <div class="checkbox">
                  <label>
                    @Html.EditorFor(m => m.Rem3SendToEmail1) Send to Email 1
                  </label>
                </div>
                <div class="form-group">
                  <label for="Rem3Email2">Email 2</label>
                  @Html.TextBoxFor(m => m.Rem3Email2, new { @class = "form-control", disabled = "disabled" })
                </div>
                <div class="checkbox">
                  <label>
                    @Html.EditorFor(m => m.Rem3SendToEmail2) Send to Email 2
                  </label>
                </div>
                <div class="form-group">
                  <label for="Rem3AdditionalEmail">Additional Email</label><br />
                  @Html.TextBoxFor(model => model.Rem3AdditionalEmail, new { @class = "form-control" })
                </div>
              </div>
              <!-- /.box-body -->

              <div class="box-footer">
                <button type="submit" id="btnSaveAlert3" name="submitButton" value="SaveAlert" class="btn btn-primary">Submit</button>
              </div>
            </form>
          </div>
          <!-- /.box -->
        </div>
        <!-- ./col -->
      </div>
      <!-- /.row -->
}

    </section>

  </div>
  <!-- /.content-wrapper -->

Controller

[HttpPost]
public ActionResult LowCreditAlert(ClientSMSAccountCustom ClientSMSAccountModal, string submitButton)
{
    //check if logged in
    if (Convert.ToInt64(Session["UserID"]) == 0)
        return Redirect("~/");

    switch (submitButton)
    {
        case "ShowAlerts":
            // delegate sending to another controller action
            return (ShowAlerts(ClientSMSAccountModal));
        case "SaveAlert":
            // call another action to perform the cancellation
            return (SaveAlert(ClientSMSAccountModal));
        default:
            // If they've submitted the form without a submitButton, 
            // just return the view again.
            return (ShowAlerts(ClientSMSAccountModal));
    }
}
private ActionResult SaveAlert(ClientSMSAccountCustom ClientSMSAccountModal)
{
    RADSystem rad = new RADSystem();

    var model = new ClientSMSAccountCustom();
    long companyId = Convert.ToInt64(Session["CompanyID"]);

    //get all countries
    var countries = rad.Countries.OrderBy(x => x.CountryID).ToList();

    #region Save

    ClientSMSAccount smsAccount = rad.ClientSMSAccounts
                                  .Where(x => (x.CompanyID == companyId) && (x.CountryID == ClientSMSAccountModal.CountryID))
                                  .FirstOrDefault();

    smsAccount.Rem1LowAmount = ClientSMSAccountModal.Rem1LowAmount;
    smsAccount.Rem1AdditionalEmail = ClientSMSAccountModal.Rem1AdditionalEmail;
    smsAccount.Rem1SendToEmail1 = ClientSMSAccountModal.Rem1SendToEmail1;
    smsAccount.Rem1SendToEmail2 = ClientSMSAccountModal.Rem1SendToEmail2;

    smsAccount.Rem2LowAmount = ClientSMSAccountModal.Rem2LowAmount;
    smsAccount.Rem2AdditioanlEmail = ClientSMSAccountModal.Rem2AdditioanlEmail;
    smsAccount.Rem2SendToEmail1 = ClientSMSAccountModal.Rem2SendToEmail1;
    smsAccount.Rem2SendToEmail2 = ClientSMSAccountModal.Rem2SendToEmail2;

    smsAccount.Rem3LowAmount = ClientSMSAccountModal.Rem3LowAmount;
    smsAccount.Rem3AdditionalEmail = ClientSMSAccountModal.Rem3AdditionalEmail;
    smsAccount.Rem3SendToEmail1 = ClientSMSAccountModal.Rem3SendToEmail1;
    smsAccount.Rem3SendToEmail2 = ClientSMSAccountModal.Rem3SendToEmail2;

    rad.ClientSMSAccounts.Attach(smsAccount);
    rad.Entry(smsAccount).State = System.Data.Entity.EntityState.Modified;
    rad.SaveChanges();
    rad.Dispose();

    #endregion

    #region Prepare model

    foreach (var c in countries)
    {
        SelectListItem item = new SelectListItem();
        item.Text = c.Description;
        item.Value = c.CountryID.ToString();
        model.Countries.Add(item);
    }

    model.Rem1AdditionalEmail = ClientSMSAccountModal.Rem1AdditionalEmail;
    model.Rem1LowAmount = ClientSMSAccountModal.Rem1LowAmount;
    model.Rem1SendToEmail1 = ClientSMSAccountModal.Rem1SendToEmail1;
    model.Rem1SendToEmail2 = ClientSMSAccountModal.Rem1SendToEmail2;
    model.Rem1Email1 = ClientSMSAccountModal.Rem1Email1;
    model.Rem1Email2 = ClientSMSAccountModal.Rem1Email2;

    model.Rem2AdditioanlEmail = ClientSMSAccountModal.Rem2AdditioanlEmail;
    model.Rem2LowAmount = ClientSMSAccountModal.Rem2LowAmount;
    model.Rem2SendToEmail1 = ClientSMSAccountModal.Rem2SendToEmail1;
    model.Rem2SendToEmail2 = ClientSMSAccountModal.Rem2SendToEmail2;
    model.Rem2Email1 = ClientSMSAccountModal.Rem2Email1;
    model.Rem2Email2 = ClientSMSAccountModal.Rem2Email2;

    model.Rem3AdditionalEmail = ClientSMSAccountModal.Rem3AdditionalEmail;
    model.Rem3LowAmount = ClientSMSAccountModal.Rem3LowAmount;
    model.Rem3SendToEmail1 = ClientSMSAccountModal.Rem3SendToEmail1;
    model.Rem3SendToEmail2 = ClientSMSAccountModal.Rem3SendToEmail2;
    model.Rem3Email1 = ClientSMSAccountModal.Rem3Email1;
    model.Rem3Email2 = ClientSMSAccountModal.Rem3Email2;

    #endregion

    return (View("LowCreditAlert", model));
}

Everything looks correct to me. The Reminder 1 alerts are saving. Reminder 2 and 3 are not saving. Also the page reloads with some blank values after posting. When I reload the page, the value come back.

derloopkat
  • 6,232
  • 16
  • 38
  • 45
Orion
  • 452
  • 6
  • 23
  • 2
    Please [edit] your question into a [mcve]. Also, "some properties" is not clear, you need to say specifically what you are getting at the action – Camilo Terevinto Jan 09 '18 at 00:30
  • also make sure your entity model is mapped correctly for the fields you're unable to save – derloopkat Jan 09 '18 at 00:54
  • 1
    You have nested forms which is invalid html, and not supported –  Jan 09 '18 at 00:54
  • 1
    You also have lots of disabled controls which do not submit values. –  Jan 09 '18 at 00:55
  • Even your model is not good, and what you should have is a collection of a view model containing just the 6 properties (and populated with 3 of that object) –  Jan 09 '18 at 00:57
  • And you POST method needs to be redirecting, not returning the view (you only do that is `ModelState` is invalid - the values you have posted have been added to `ModelState` and its those values that the `HtmlHelper` methods use, not the updated values that you set in the POST method. –  Jan 09 '18 at 00:59
  • @StephenMuecke - after removing the nested forms, the values are now saving. Thank you for pointing that out. With the disabled controls, I think I will need to retrieve them from the database after saving, so the form loads with the values. – Orion Jan 09 '18 at 01:13
  • But the form will not display those values because you using `TextBoxFor()` and you have returned the view so the value that will be displayed will be the value you have posted which is `null` (refer [this answer](https://stackoverflow.com/questions/26654862/textboxfor-displaying-initial-value-not-the-value-updated-from-code/26664111#26664111) for an explanation). Since you do not want to edit them, just display them as text (and style it to look like a textbox if that is what you want) –  Jan 09 '18 at 01:16
  • @StephenMuecke I'm re-populating the model for the disabled controls with values from the database when returning the view. It's now displaying again after saving. – Orion Jan 09 '18 at 01:22

1 Answers1

0

After removing the nested forms, the values are now saving.

For the disabled controls. I am re-populating the values when returning the view after submitting the data.

Orion
  • 452
  • 6
  • 23