MVC C#仅保存模型的一部分

时间:2018-01-09 00:26:03

标签: c# asp.net-mvc entity-framework

我的保存方法发生了这种奇怪的行为。只保存表的一部分,其余部分不保存。似乎模型没有发布我遗漏的所有值或其他东西。

模型

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; }
    }
}

查看

@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 -->

控制器

[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));
}

一切看起来都对我不对。提醒1警报正在保存。提醒2和3未保存。此页面在发布后重新加载一些空白值。当我重新加载页面时,值会回来。

1 个答案:

答案 0 :(得分:0)

删除嵌套表单后,值现在正在保存。

对于禁用的控件。我在提交数据后返回视图时重新填充值。