在 MVC 项目中创建和编辑帐户的问题
Problems with creating and editing accounts in MVC project
我需要能够创建新帐户(基于我的模型)以及编辑现有帐户。我正在努力尝试这个,在我看来,我的控制器中的一个 ActionResult 函数(CadastrarContas)没有正常工作。当我在完成表格后尝试创建一个新帐户时,数据库中没有任何反应并且我的主视图 returns 没有结果。至于编辑现有帐户,我没有得到任何更改,而且我的一些现有帐户在主要 view.Can 中丢失了,有人给我一些见解吗?我将提供我的代码并尽量做到清晰,如果需要更多代码,我将编辑 post。提前致谢。
型号
public class Cobrancas_Contas
{
[Key]
[Display(Name = "Código")]
public int ContaID { get; set; }
[Required(ErrorMessage = " - Campo Obrigatório")]
[Display(Name = "Empresa")]
public int EmpresaID { get; set; }
[Display(Name = "Filial")]
public int FilialID { get; set; }
[Required(ErrorMessage = " - Campo Obrigatório")]
[Display(Name = "Cedente")]
public string Nome { get; set; }
[Required(ErrorMessage = " - Campo Obrigatório")]
[Display(Name = "Documento")]
public string Documento { get; set; }
[Required(ErrorMessage = " - Campo Obrigatório")]
[Display(Name = "Banco")]
public string Banco { get; set; }
[Required(ErrorMessage = " - Campo Obrigatório")]
[Display(Name = "Agência")]
public string Agencia { get; set; }
[Required(ErrorMessage = " - Campo Obrigatório")]
[Display(Name = "Agência - DV")]
public string AgenciaDV { get; set; }
[Required(ErrorMessage = " - Campo Obrigatório")]
[Display(Name = "Conta")]
public string Conta { get; set; }
[Required(ErrorMessage = " - Campo Obrigatório")]
[Display(Name = "DV - Conta")]
public string ContaDV { get; set; }
[Required(ErrorMessage = " - Campo Obrigatório")]
[Display(Name = "Convênio SICOV")]
public int Convenio { get; set; }
[Required(ErrorMessage = " - Campo Obrigatório")]
[Display(Name = "Convênio SIACC")]
public int ConvenioSIACC { get; set; }
[Required(ErrorMessage = " - Campo Obrigatório")]
[Display(Name = "Tipo Convênio")]
public string TipoConvenio { get; set; }
[Required(ErrorMessage = " - Campo Obrigatório")]
[Display(Name = "Variação")]
public int Variacao { get; set; }
[Required(ErrorMessage = " - Campo Obrigatório")]
[Display(Name = "Tipo")]
public string Tipo { get; set; }
[Required(ErrorMessage = " - Campo Obrigatório")]
[Display(Name = "Tipo de Cobrança")]
public string Cobranca { get; set; }
[Required(ErrorMessage = " - Campo Obrigatório")]
[Display(Name = "Especie Padrão")]
public string EspeciePadrao { get; set; }
[Required(ErrorMessage = " - Campo Obrigatório")]
[Display(Name = "Indicativo Padrão")]
public string IndicativoPadrao { get; set; }
[Required(ErrorMessage = " - Campo Obrigatório")]
[Display(Name = "Código Multa")]
public int CodigoMulta { get; set; }
[Required(ErrorMessage = " - Campo Obrigatório")]
[Display(Name = "Carteira")]
public int Carteira { get; set; }
[Required(ErrorMessage = " - Campo Obrigatório")]
[Display(Name = "Dias Multa")]
public int DiasMulta { get; set; }
[Required(ErrorMessage = " - Campo Obrigatório")]
[Display(Name = "Dias Baixa")]
public int DiasBaixa { get; set; }
[Required(ErrorMessage = " - Campo Obrigatório")]
[Display(Name = "Percentual de Juros")]
public decimal PercentualJuros { get; set; }
[Required(ErrorMessage = " - Campo Obrigatório")]
[Display(Name = "Percentual de Multa")]
public decimal PercentualMultas { get; set; }
[Display(Name = "Sequencial")]
public int Sequencial { get; set; }
[Display(Name = "Sequencial SICOV")]
public int SequencialDDA { get; set; }
[Display(Name = "Sequencial SIACC")]
public int SequencialSIACC { get; set; }
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy HH:mm:ss}", ApplyFormatInEditMode = true)]
public DateTime Cadastro { get; set; }
[Required(ErrorMessage = " - Campo Obrigatório")]
[Display(Name = "Situação")]
public string Situacao { get; set; }
[Display(Name = "Operação")]
public string Operacao { get; set; }
public virtual Empresas Empresas { get; set; }
public virtual Filiais Filiais { get; set; }
}
主视图
<div class="row MAS ADM">
<div class="col-md-12 grid">
<table class="table table-bordered table-striped">
<tr>
<th class="text-center">ID</th>
<th class="text-center">Banco</th>
<th class="text-center">Agência</th>
<th class="text-center">Conta</th>
<th class="text-center">Cobrança</th>
<th class="text-center">Situação</th>
<th class="text-center">Operação</th>
<th class="text-center">Ação</th>
</tr>
@foreach (var item in Model)
{
CountLinhas += 1;
int ContaID = item.ContaID;
<tr>
<td class="text-center">@ContaID.ToString("0000")</td>
<td class="text-center">@Html.DisplayFor(modelItem => item.Banco) </td>
<td class="text-center">@Html.DisplayFor(modelItem => item.Agencia) </td>
<td class="text-center">@Html.DisplayFor(modelItem => item.Conta) </td>
<td class="text-center">@Html.DisplayFor(modelItem => item.Cobranca)</td>
<td class="text-center">@Html.DisplayFor(modelItem => item.Situacao)</td>
<td class="text-center">@Html.DisplayFor(modelItem => item.Operacao)</td>
<td class="text-center">
<a id="bt-@CountLinhas.ToString("0000")" href="/Administrativo/GetContasFiliais/?CtID=@item.ContaID" data-window="#Editar-Open" class="btn btn-default btn-xs bt-contas" title="Editar filiais desta conta"><span class="glyphicon glyphicon-transfer"></span></a>
<a href="../Contas/Form/@item.ContaID?acao=Editar" class="btn btn-default btn-xs bt-contas" title="Editar Conta"><span class="glyphicon glyphicon-edit"></span></a>
</td>
</tr>
}
</table>
</div>
</div>
窗体视图
<div id="Form" class="form-container @ViewBag.Acao">
<div class="page-header">
<h1>Cadastro de Contas</h1>
</div>
@using (Html.BeginForm(Acao, "Administrativo/Contas", FormMethod.Post, new { id = "FRMContas" }))
{
@Html.AntiForgeryToken()
<div class="panel panel-primary">
<div class="panel-heading">
<h4>Preencha com as informações do formulário!</h4>
</div>
<div class="panel-body">
@Html.HiddenFor(model => Model.ContaID)
@if (Area == "MAS" || Area == "ADM")
{
<div class="form-group col-md-6">
@Html.LabelFor(model => model.EmpresaID, htmlAttributes: new { @class = "control-label" }) <!--Cria tag Label-->
@Html.ValidationMessageFor(model => model.EmpresaID, "", new { @class = "text-danger" }) <!--Cria tag span-->
@Html.DropDownListFor(model => model.EmpresaID, (List<SelectListItem>)lEmpresas, " - SELECIONE - ", new { @class = "form-control" }) <!--Cria tag select-->
</div>
}
<div class="form-group col-md-3 CampoForm">
@Html.LabelFor(model => model.Documento, htmlAttributes: new { @class = "control-label" })
@Html.ValidationMessageFor(model => model.ContaID, "", new { @class = "text-danger" })
@Html.EditorFor(model => model.ContaID, new { htmlAttributes = new { @class = "form-control"} })
</div>
<div class="form-group col-md-3 CampoForm">
@Html.LabelFor(model => model.Nome, htmlAttributes: new { @class = "control-label" })
@Html.ValidationMessageFor(model => model.Nome, "", new { @class = "text-danger" })
@Html.EditorFor(model => model.Nome, new { htmlAttributes = new { @class = "form-control", placeholder = "Nome" } })
</div>
<div class="form-group col-md-3 CampoForm">
@Html.LabelFor(model => model.Banco, htmlAttributes: new { @class = "control-label" })
@Html.ValidationMessageFor(model => model.Banco, "", new { @class = "text-danger" })
@Html.EditorFor(model => model.Banco, new { htmlAttributes = new { @class = "form-control", value = "", placeholder = "000", data_tipo = "numerico", maxlength = 3 } })
@Html.DropDownList("nomeBancos_", (List<SelectListItem>)lBancos, " - Selecione um banco -", new { @class = "form-control col-md-10" })
</div>
<div class="form-group col-md-3 CampoForm">
@Html.LabelFor(model => model.Agencia, htmlAttributes: new { @class = "control-label" })
@Html.ValidationMessageFor(model => model.Agencia, "", new { @class = "text-danger" })
@Html.EditorFor(model => model.Agencia, new { htmlAttributes = new { @class = "form-control", placeholder = "0000", data_tipo = "numerico" , maxlength = 4} })
</div>
<div class="form-group col-md-3 CampoForm">
@Html.LabelFor(model => model.AgenciaDV, htmlAttributes: new { @class = "control-label" })
@Html.ValidationMessageFor(model => model.AgenciaDV, "", new { @class = "text-danger" })
@Html.EditorFor(model => model.AgenciaDV, new { htmlAttributes = new { @class = "form-control", placeholder = "0", data_tipo = "numerico", maxlength = 1 } })
</div>
<div class="form-group col-md-3 CampoForm">
@Html.LabelFor(model => model.Conta, htmlAttributes: new { @class = "control-label" })
@Html.ValidationMessageFor(model => model.Conta, "", new { @class = "text-danger" })
@Html.EditorFor(model => model.Conta, new { htmlAttributes = new { @class = "form-control", placeholder = "00000000", data_tipo = "numerico", maxlength = 8 } })
</div>
<div class="form-group col-md-3 CampoForm">
@Html.LabelFor(model => model.ContaDV, htmlAttributes: new { @class = "control-label" })
@Html.ValidationMessageFor(model => model.ContaDV, "", new { @class = "text-danger" })
@Html.EditorFor(model => model.ContaDV, new { htmlAttributes = new { @class = "form-control", placeholder = "0", data_tipo = "numerico", maxlength = 1 } })
</div>
<div class="form-group col-md-3 CampoForm">
@Html.LabelFor(model => model.Convenio, htmlAttributes: new { @class = "control-label" })
@Html.ValidationMessageFor(model => model.Convenio, "", new { @class = "text-danger" })
@Html.EditorFor(model => model.Convenio, new { htmlAttributes = new { @class = "form-control", placeholder = "Convênio", data_tipo = "numerico" } })
</div>
<div class="form-group col-md-3 CampoForm">
@Html.LabelFor(model => model.ConvenioSIACC, htmlAttributes: new { @class = "control-label" })
@Html.ValidationMessageFor(model => model.ConvenioSIACC, "", new { @class = "text-danger" })
@Html.EditorFor(model => model.ConvenioSIACC, new { htmlAttributes = new { @class = "form-control", data_tipo = "numerico" } })
</div>
<div class="form-group col-md-3 CampoForm">
@Html.LabelFor(model => model.TipoConvenio, htmlAttributes: new { @class = "control-label" })
@Html.ValidationMessageFor(model => model.TipoConvenio, "", new { @class = "text-danger" })
@Html.EditorFor(model => model.TipoConvenio, new { htmlAttributes = new { @class = "form-control", maxlength = 10} })
</div>
<div class="form-group col-md-3 CampoForm">
@Html.LabelFor(model => model.Variacao, htmlAttributes: new { @class = "control-label" })
@Html.ValidationMessageFor(model => model.Variacao, "", new { @class = "text-danger" })
@Html.EditorFor(model => model.Variacao, new { htmlAttributes = new { @class = "form-control", data_tipo = "numerico" } })
</div>
<div class="form-group col-md-3 CampoForm">
@Html.LabelFor(model => model.Tipo, htmlAttributes: new { @class = "control-label" })
@Html.ValidationMessageFor(model => model.Tipo, "", new { @class = "text-danger" })
@Html.EditorFor(model => model.Tipo, new { htmlAttributes = new { @class = "form-control", maxlength = 5 } })
</div>
<div class="form-group col-md-3 CampoForm">
@Html.LabelFor(model => model.Cobranca, htmlAttributes: new { @class = "control-label" })
@Html.ValidationMessageFor(model => model.Cobranca, "", new { @class = "text-danger" })
@Html.EditorFor(model => model.Cobranca, new { htmlAttributes = new { @class = "form-control" } })
</div>
<div class="form-group col-md-3 CampoForm">
@Html.LabelFor(model => model.EspeciePadrao, htmlAttributes: new { @class = "control-label" })
@Html.ValidationMessageFor(model => model.EspeciePadrao, "", new { @class = "text-danger" })
@Html.EditorFor(model => model.EspeciePadrao, new { htmlAttributes = new { @class = "form-control", maxlength = 6 } })
</div>
<div class="form-group col-md-3 CampoForm">
@Html.LabelFor(model => model.IndicativoPadrao, htmlAttributes: new { @class = "control-label" })
@Html.ValidationMessageFor(model => model.IndicativoPadrao, "", new { @class = "text-danger" })
@Html.EditorFor(model => model.IndicativoPadrao, new { htmlAttributes = new { @class = "form-control", maxlength = 6 } })
</div>
<div class="form-group col-md-3 CampoForm">
@Html.LabelFor(model => model.CodigoMulta, htmlAttributes: new { @class = "control-label" })
@Html.ValidationMessageFor(model => model.CodigoMulta, "", new { @class = "text-danger" })
@Html.EditorFor(model => model.CodigoMulta, new { htmlAttributes = new { @class = "form-control", data_tipo = "numerico" } })
</div>
<div class="form-group col-md-3 CampoForm">
@Html.LabelFor(model => model.Carteira, htmlAttributes: new { @class = "control-label" })
@Html.ValidationMessageFor(model => model.Carteira, "", new { @class = "text-danger" })
@Html.EditorFor(model => model.Carteira, new { htmlAttributes = new { @class = "form-control", data_tipo = "numerico" } })
</div>
<div class="form-group col-md-3 CampoForm">
@Html.LabelFor(model => model.DiasMulta, htmlAttributes: new { @class = "control-label" })
@Html.ValidationMessageFor(model => model.DiasMulta, "", new { @class = "text-danger" })
@Html.EditorFor(model => model.DiasMulta, new { htmlAttributes = new { @class = "form-control", data_tipo = "numerico" } })
</div>
<div class="form-group col-md-3 CampoForm">
@Html.LabelFor(model => model.DiasBaixa, htmlAttributes: new { @class = "control-label" })
@Html.ValidationMessageFor(model => model.DiasBaixa, "", new { @class = "text-danger" })
@Html.EditorFor(model => model.DiasBaixa, new { htmlAttributes = new { @class = "form-control", data_tipo = "numerico" } })
</div>
<div class="form-group col-md-3 CampoForm">
@Html.LabelFor(model => model.PercentualJuros, htmlAttributes: new { @class = "control-label" })
@Html.ValidationMessageFor(model => model.PercentualJuros, "", new { @class = "text-danger" })
@Html.EditorFor(model => model.PercentualJuros, new { htmlAttributes = new { @class = "form-control", data_tipo = "decimal" } })
</div>
<div class="form-group col-md-3 CampoForm">
@Html.LabelFor(model => model.Sequencial, htmlAttributes: new { @class = "control-label" })
@Html.ValidationMessageFor(model => model.Sequencial, "", new { @class = "text-danger" })
@Html.EditorFor(model => model.Sequencial, new { htmlAttributes = new { @class = "form-control", data_tipo = "numerico" } })
</div>
<div class="form-group col-md-3 CampoForm">
@Html.LabelFor(model => model.SequencialDDA, htmlAttributes: new { @class = "control-label" })
@Html.ValidationMessageFor(model => model.SequencialDDA, "", new { @class = "text-danger" })
@Html.EditorFor(model => model.SequencialDDA, new { htmlAttributes = new { @class = "form-control", data_tipo = "numerico" } })
</div>
<div class="form-group col-md-3 CampoForm">
@Html.LabelFor(model => model.SequencialSIACC, htmlAttributes: new { @class = "control-label" })
@Html.ValidationMessageFor(model => model.SequencialSIACC, "", new { @class = "text-danger" })
@Html.EditorFor(model => model.SequencialSIACC, new { htmlAttributes = new { @class = "form-control", data_tipo = "numerico" } })
</div>
<div class="form-group col-md-3 CampoForm">
@Html.LabelFor(model => model.Operacao, htmlAttributes: new { @class = "control-label" })
@Html.ValidationMessageFor(model => model.Operacao, "", new { @class = "text-danger" })
@Html.EditorFor(model => model.Operacao, new { htmlAttributes = new { @class = "form-control", maxlength = 4 } })
</div>
<div class="form-group col-md-3 CampoForm">
<label class="form-label">Cadastro</label>
<input id="Cadastro" name="Cadastro" type="text" readonly class="form-control" value="@Cadastro" />
</div>
<div class="form-group col-md-3 CampoForm">
@Html.LabelFor(model => model.Situacao, htmlAttributes: new { @class = "control-label" })
@Html.ValidationMessageFor(model => model.Situacao, "", new { @class = "text-danger", maxlength = 1 })
@Html.DropDownListFor(model => model.EmpresaID, (List<SelectListItem>)lSituacao, " - SELECIONE - ", new { @class = "form-control" })
</div>
</div>
<div class="panel-footer">
<input type="submit" value="@ViewBag.Label" class="btn btn-primary"> | @Html.ActionLink("Cancelar", "../Administrativo/Contas/", new { id = "", acao = "" }, new { @class = "btn btn-primary" })
</div>
</div>
}
</div>
控制器
public ActionResult Contas(int pagina = 1, int EmID = 0, int FiID = 0, string Conta = "", string Situacao = "")
{
bool IsLogado = (Session["IsLogado"] is null);
if (IsLogado)
{
return RedirectToAction("Login", "User", new { acao = "ErroSessao" });
};
string Area = Session["Area"].ToString();
int UserID = Convert.ToInt32(Session["UserID"]);
int EmpresaID = Convert.ToInt32(Session["EmpresaID"]);
int FilialID = Convert.ToInt32(Session["FilialID"]);
int referenciaID = PageList.GetReferenciaID(Area, FilialID, EmpresaID, 0, 0);
referenciaID = (Area == "MAS") ? UserID : referenciaID;
EmID = (EmID == 0) ? EmpresaID : EmID;
FiID = (FiID == 0) ? FilialID : FiID;
ViewBag.EmID = EmID;
ViewBag.FiID = FiID;
ViewBag.Conta = Conta;
ViewBag.Situacao = Situacao;
//Definindo a paginação
int paginaQdteRegistros = (Session["RegistrosPorPagina"] == null) ? 10 : Convert.ToInt32(Session["RegistrosPorPagina"]);
int paginaNumeroNavegacao = pagina;
IPagedList Contas = PageList.GetAdmContas(db, paginaNumeroNavegacao, paginaQdteRegistros, Area, EmID, FiID, Conta, Situacao);
return View(Contas);
}
public ActionResult FormContas(int id = 0, string acao = "")
{
bool IsLogado = (Session["IsLogado"] is null);
if (IsLogado)
{
return RedirectToAction("Login", "User", new { acao = "ErroSessao" });
};
ViewBag.Id = id;
ViewBag.Acao = acao;
ViewBag.Label = "Cadastrar";
if (acao == "Editar") { ViewBag.Label = "Atualizar"; };
if (id == 0)
{
ViewBag.EmID = 0;
ViewBag.FiID = 0;
return View();
}
Cobrancas_Contas CobContas = DBCtx.Cob_ContasDB.Find(id);
ViewBag.EmID = CobContas.Filiais.EmpresaID;
if (CobContas == null)
{
return View();
}
return View(CobContas);
}
[Route("Administrativo/Contas/Cadastrar/")]
public ActionResult CadastrarContas(Cobrancas_Contas CobContas)
{
bool IsLogado = (Session["IsLogado"] is null);
if (IsLogado)
{
return RedirectToAction("Login", "User", new { acao = "ErroSessao" });
};
if (!ModelState.IsValid)
{
return View("FormContas", CobContas);
}
int ContaID = CobContas.ContaID;
if (ContaID == 0)
{
DBCtx.Cob_ContasDB.Add(CobContas);
}
else
{
DBCtx.Entry(CobContas).State = EntityState.Modified;
}
DBCtx.SaveChanges();
ViewModelSucesso ViewMS = new ViewModelSucesso();
ViewMS.Controller = "Administrativo";
ViewMS.View = "Contas";
return View("Sucesso", ViewMS);
}
路由配置
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapRoute(
name: "AdmContasFRM",
url: "Administrativo/Contas/Form/{id}",
defaults: new { controller = "Empresas", action = "FormContas", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "AdmContasCadastrar",
url: "Administrativo/Contas/Cadastrar/",
defaults: new { controller = "Empresas", action = "CadastrarContas"}
);
routes.MapRoute(
name: "AdmContas",
url: "Administrativo/{action}/{id}",
defaults: new { controller = "Empresas", action = "Contas", id = UrlParameter.Optional, busca = UrlParameter.Optional, vencDe = UrlParameter.Optional, vencAte = UrlParameter.Optional, pagina = UrlParameter.Optional }
);
}
}
问题是路由冲突,在我的配置中我有这个:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapRoute(
name: "AdmIndicesFRM",
url: "Administrativo/Indices/Form/{id}",
defaults: new { controller = "Empresas", action = "FormIndices", id = UrlParameter.Optional}
);
routes.MapRoute(
name: "AdmIndicesCadastrar",
url: "Administrativo/Indices/Cadastrar/",
defaults: new { controller = "Empresas", action = "CadastrarIndices" }
);
routes.MapRoute(
name: "AdmIndices",
url: "Administrativo/{action}/{id}",
defaults: new { controller = "Empresas", action = "Indices", id = UrlParameter.Optional, busca = UrlParameter.Optional, vencDe = UrlParameter.Optional, vencAte = UrlParameter.Optional, pagina = UrlParameter.Optional }
);
routes.MapRoute(
name: "AdmContasFRM",
url: "Administrativo/Contas/Form/{id}",
defaults: new { controller = "Empresas", action = "FormContas", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "AdmContasCadastrar",
url: "Administrativo/Contas/Cadastrar/",
defaults: new { controller = "Empresas", action = "CadastrarContas"}
);
routes.MapRoute(
name: "AdmContas",
url: "Administrativo/{action}/{id}",
defaults: new { controller = "Empresas", action = "Contas", id = UrlParameter.Optional, busca = UrlParameter.Optional, vencDe = UrlParameter.Optional, vencAte = UrlParameter.Optional, pagina = UrlParameter.Optional }
);
}
}
我把声明顺序改成这样,我的问题就解决了
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapRoute(
name: "AdmIndicesFRM",
url: "Administrativo/Indices/Form/{id}",
defaults: new { controller = "Empresas", action = "FormIndices", id = UrlParameter.Optional}
);
routes.MapRoute(
name: "AdmIndicesCadastrar",
url: "Administrativo/Indices/Cadastrar/",
defaults: new { controller = "Empresas", action = "CadastrarIndices" }
);
routes.MapRoute(
name: "AdmContasFRM",
url: "Administrativo/Contas/Form/{id}",
defaults: new { controller = "Empresas", action = "FormContas", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "AdmContasCadastrar",
url: "Administrativo/Contas/Cadastrar/",
defaults: new { controller = "Empresas", action = "CadastrarContas"}
);
routes.MapRoute(
name: "AdmContas",
url: "Administrativo/Contas/{id}",
defaults: new { controller = "Empresas", action = "Contas", id = UrlParameter.Optional, busca = UrlParameter.Optional, vencDe = UrlParameter.Optional, vencAte = UrlParameter.Optional, pagina = UrlParameter.Optional }
);
routes.MapRoute(
name: "AdmIndices",
url: "Administrativo/{action}/{id}",
defaults: new { controller = "Empresas", action = "Indices", id = UrlParameter.Optional, busca = UrlParameter.Optional, vencDe = UrlParameter.Optional, vencAte = UrlParameter.Optional, pagina = UrlParameter.Optional }
);
}
}
我需要能够创建新帐户(基于我的模型)以及编辑现有帐户。我正在努力尝试这个,在我看来,我的控制器中的一个 ActionResult 函数(CadastrarContas)没有正常工作。当我在完成表格后尝试创建一个新帐户时,数据库中没有任何反应并且我的主视图 returns 没有结果。至于编辑现有帐户,我没有得到任何更改,而且我的一些现有帐户在主要 view.Can 中丢失了,有人给我一些见解吗?我将提供我的代码并尽量做到清晰,如果需要更多代码,我将编辑 post。提前致谢。
型号
public class Cobrancas_Contas
{
[Key]
[Display(Name = "Código")]
public int ContaID { get; set; }
[Required(ErrorMessage = " - Campo Obrigatório")]
[Display(Name = "Empresa")]
public int EmpresaID { get; set; }
[Display(Name = "Filial")]
public int FilialID { get; set; }
[Required(ErrorMessage = " - Campo Obrigatório")]
[Display(Name = "Cedente")]
public string Nome { get; set; }
[Required(ErrorMessage = " - Campo Obrigatório")]
[Display(Name = "Documento")]
public string Documento { get; set; }
[Required(ErrorMessage = " - Campo Obrigatório")]
[Display(Name = "Banco")]
public string Banco { get; set; }
[Required(ErrorMessage = " - Campo Obrigatório")]
[Display(Name = "Agência")]
public string Agencia { get; set; }
[Required(ErrorMessage = " - Campo Obrigatório")]
[Display(Name = "Agência - DV")]
public string AgenciaDV { get; set; }
[Required(ErrorMessage = " - Campo Obrigatório")]
[Display(Name = "Conta")]
public string Conta { get; set; }
[Required(ErrorMessage = " - Campo Obrigatório")]
[Display(Name = "DV - Conta")]
public string ContaDV { get; set; }
[Required(ErrorMessage = " - Campo Obrigatório")]
[Display(Name = "Convênio SICOV")]
public int Convenio { get; set; }
[Required(ErrorMessage = " - Campo Obrigatório")]
[Display(Name = "Convênio SIACC")]
public int ConvenioSIACC { get; set; }
[Required(ErrorMessage = " - Campo Obrigatório")]
[Display(Name = "Tipo Convênio")]
public string TipoConvenio { get; set; }
[Required(ErrorMessage = " - Campo Obrigatório")]
[Display(Name = "Variação")]
public int Variacao { get; set; }
[Required(ErrorMessage = " - Campo Obrigatório")]
[Display(Name = "Tipo")]
public string Tipo { get; set; }
[Required(ErrorMessage = " - Campo Obrigatório")]
[Display(Name = "Tipo de Cobrança")]
public string Cobranca { get; set; }
[Required(ErrorMessage = " - Campo Obrigatório")]
[Display(Name = "Especie Padrão")]
public string EspeciePadrao { get; set; }
[Required(ErrorMessage = " - Campo Obrigatório")]
[Display(Name = "Indicativo Padrão")]
public string IndicativoPadrao { get; set; }
[Required(ErrorMessage = " - Campo Obrigatório")]
[Display(Name = "Código Multa")]
public int CodigoMulta { get; set; }
[Required(ErrorMessage = " - Campo Obrigatório")]
[Display(Name = "Carteira")]
public int Carteira { get; set; }
[Required(ErrorMessage = " - Campo Obrigatório")]
[Display(Name = "Dias Multa")]
public int DiasMulta { get; set; }
[Required(ErrorMessage = " - Campo Obrigatório")]
[Display(Name = "Dias Baixa")]
public int DiasBaixa { get; set; }
[Required(ErrorMessage = " - Campo Obrigatório")]
[Display(Name = "Percentual de Juros")]
public decimal PercentualJuros { get; set; }
[Required(ErrorMessage = " - Campo Obrigatório")]
[Display(Name = "Percentual de Multa")]
public decimal PercentualMultas { get; set; }
[Display(Name = "Sequencial")]
public int Sequencial { get; set; }
[Display(Name = "Sequencial SICOV")]
public int SequencialDDA { get; set; }
[Display(Name = "Sequencial SIACC")]
public int SequencialSIACC { get; set; }
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy HH:mm:ss}", ApplyFormatInEditMode = true)]
public DateTime Cadastro { get; set; }
[Required(ErrorMessage = " - Campo Obrigatório")]
[Display(Name = "Situação")]
public string Situacao { get; set; }
[Display(Name = "Operação")]
public string Operacao { get; set; }
public virtual Empresas Empresas { get; set; }
public virtual Filiais Filiais { get; set; }
}
主视图
<div class="row MAS ADM">
<div class="col-md-12 grid">
<table class="table table-bordered table-striped">
<tr>
<th class="text-center">ID</th>
<th class="text-center">Banco</th>
<th class="text-center">Agência</th>
<th class="text-center">Conta</th>
<th class="text-center">Cobrança</th>
<th class="text-center">Situação</th>
<th class="text-center">Operação</th>
<th class="text-center">Ação</th>
</tr>
@foreach (var item in Model)
{
CountLinhas += 1;
int ContaID = item.ContaID;
<tr>
<td class="text-center">@ContaID.ToString("0000")</td>
<td class="text-center">@Html.DisplayFor(modelItem => item.Banco) </td>
<td class="text-center">@Html.DisplayFor(modelItem => item.Agencia) </td>
<td class="text-center">@Html.DisplayFor(modelItem => item.Conta) </td>
<td class="text-center">@Html.DisplayFor(modelItem => item.Cobranca)</td>
<td class="text-center">@Html.DisplayFor(modelItem => item.Situacao)</td>
<td class="text-center">@Html.DisplayFor(modelItem => item.Operacao)</td>
<td class="text-center">
<a id="bt-@CountLinhas.ToString("0000")" href="/Administrativo/GetContasFiliais/?CtID=@item.ContaID" data-window="#Editar-Open" class="btn btn-default btn-xs bt-contas" title="Editar filiais desta conta"><span class="glyphicon glyphicon-transfer"></span></a>
<a href="../Contas/Form/@item.ContaID?acao=Editar" class="btn btn-default btn-xs bt-contas" title="Editar Conta"><span class="glyphicon glyphicon-edit"></span></a>
</td>
</tr>
}
</table>
</div>
</div>
窗体视图
<div id="Form" class="form-container @ViewBag.Acao">
<div class="page-header">
<h1>Cadastro de Contas</h1>
</div>
@using (Html.BeginForm(Acao, "Administrativo/Contas", FormMethod.Post, new { id = "FRMContas" }))
{
@Html.AntiForgeryToken()
<div class="panel panel-primary">
<div class="panel-heading">
<h4>Preencha com as informações do formulário!</h4>
</div>
<div class="panel-body">
@Html.HiddenFor(model => Model.ContaID)
@if (Area == "MAS" || Area == "ADM")
{
<div class="form-group col-md-6">
@Html.LabelFor(model => model.EmpresaID, htmlAttributes: new { @class = "control-label" }) <!--Cria tag Label-->
@Html.ValidationMessageFor(model => model.EmpresaID, "", new { @class = "text-danger" }) <!--Cria tag span-->
@Html.DropDownListFor(model => model.EmpresaID, (List<SelectListItem>)lEmpresas, " - SELECIONE - ", new { @class = "form-control" }) <!--Cria tag select-->
</div>
}
<div class="form-group col-md-3 CampoForm">
@Html.LabelFor(model => model.Documento, htmlAttributes: new { @class = "control-label" })
@Html.ValidationMessageFor(model => model.ContaID, "", new { @class = "text-danger" })
@Html.EditorFor(model => model.ContaID, new { htmlAttributes = new { @class = "form-control"} })
</div>
<div class="form-group col-md-3 CampoForm">
@Html.LabelFor(model => model.Nome, htmlAttributes: new { @class = "control-label" })
@Html.ValidationMessageFor(model => model.Nome, "", new { @class = "text-danger" })
@Html.EditorFor(model => model.Nome, new { htmlAttributes = new { @class = "form-control", placeholder = "Nome" } })
</div>
<div class="form-group col-md-3 CampoForm">
@Html.LabelFor(model => model.Banco, htmlAttributes: new { @class = "control-label" })
@Html.ValidationMessageFor(model => model.Banco, "", new { @class = "text-danger" })
@Html.EditorFor(model => model.Banco, new { htmlAttributes = new { @class = "form-control", value = "", placeholder = "000", data_tipo = "numerico", maxlength = 3 } })
@Html.DropDownList("nomeBancos_", (List<SelectListItem>)lBancos, " - Selecione um banco -", new { @class = "form-control col-md-10" })
</div>
<div class="form-group col-md-3 CampoForm">
@Html.LabelFor(model => model.Agencia, htmlAttributes: new { @class = "control-label" })
@Html.ValidationMessageFor(model => model.Agencia, "", new { @class = "text-danger" })
@Html.EditorFor(model => model.Agencia, new { htmlAttributes = new { @class = "form-control", placeholder = "0000", data_tipo = "numerico" , maxlength = 4} })
</div>
<div class="form-group col-md-3 CampoForm">
@Html.LabelFor(model => model.AgenciaDV, htmlAttributes: new { @class = "control-label" })
@Html.ValidationMessageFor(model => model.AgenciaDV, "", new { @class = "text-danger" })
@Html.EditorFor(model => model.AgenciaDV, new { htmlAttributes = new { @class = "form-control", placeholder = "0", data_tipo = "numerico", maxlength = 1 } })
</div>
<div class="form-group col-md-3 CampoForm">
@Html.LabelFor(model => model.Conta, htmlAttributes: new { @class = "control-label" })
@Html.ValidationMessageFor(model => model.Conta, "", new { @class = "text-danger" })
@Html.EditorFor(model => model.Conta, new { htmlAttributes = new { @class = "form-control", placeholder = "00000000", data_tipo = "numerico", maxlength = 8 } })
</div>
<div class="form-group col-md-3 CampoForm">
@Html.LabelFor(model => model.ContaDV, htmlAttributes: new { @class = "control-label" })
@Html.ValidationMessageFor(model => model.ContaDV, "", new { @class = "text-danger" })
@Html.EditorFor(model => model.ContaDV, new { htmlAttributes = new { @class = "form-control", placeholder = "0", data_tipo = "numerico", maxlength = 1 } })
</div>
<div class="form-group col-md-3 CampoForm">
@Html.LabelFor(model => model.Convenio, htmlAttributes: new { @class = "control-label" })
@Html.ValidationMessageFor(model => model.Convenio, "", new { @class = "text-danger" })
@Html.EditorFor(model => model.Convenio, new { htmlAttributes = new { @class = "form-control", placeholder = "Convênio", data_tipo = "numerico" } })
</div>
<div class="form-group col-md-3 CampoForm">
@Html.LabelFor(model => model.ConvenioSIACC, htmlAttributes: new { @class = "control-label" })
@Html.ValidationMessageFor(model => model.ConvenioSIACC, "", new { @class = "text-danger" })
@Html.EditorFor(model => model.ConvenioSIACC, new { htmlAttributes = new { @class = "form-control", data_tipo = "numerico" } })
</div>
<div class="form-group col-md-3 CampoForm">
@Html.LabelFor(model => model.TipoConvenio, htmlAttributes: new { @class = "control-label" })
@Html.ValidationMessageFor(model => model.TipoConvenio, "", new { @class = "text-danger" })
@Html.EditorFor(model => model.TipoConvenio, new { htmlAttributes = new { @class = "form-control", maxlength = 10} })
</div>
<div class="form-group col-md-3 CampoForm">
@Html.LabelFor(model => model.Variacao, htmlAttributes: new { @class = "control-label" })
@Html.ValidationMessageFor(model => model.Variacao, "", new { @class = "text-danger" })
@Html.EditorFor(model => model.Variacao, new { htmlAttributes = new { @class = "form-control", data_tipo = "numerico" } })
</div>
<div class="form-group col-md-3 CampoForm">
@Html.LabelFor(model => model.Tipo, htmlAttributes: new { @class = "control-label" })
@Html.ValidationMessageFor(model => model.Tipo, "", new { @class = "text-danger" })
@Html.EditorFor(model => model.Tipo, new { htmlAttributes = new { @class = "form-control", maxlength = 5 } })
</div>
<div class="form-group col-md-3 CampoForm">
@Html.LabelFor(model => model.Cobranca, htmlAttributes: new { @class = "control-label" })
@Html.ValidationMessageFor(model => model.Cobranca, "", new { @class = "text-danger" })
@Html.EditorFor(model => model.Cobranca, new { htmlAttributes = new { @class = "form-control" } })
</div>
<div class="form-group col-md-3 CampoForm">
@Html.LabelFor(model => model.EspeciePadrao, htmlAttributes: new { @class = "control-label" })
@Html.ValidationMessageFor(model => model.EspeciePadrao, "", new { @class = "text-danger" })
@Html.EditorFor(model => model.EspeciePadrao, new { htmlAttributes = new { @class = "form-control", maxlength = 6 } })
</div>
<div class="form-group col-md-3 CampoForm">
@Html.LabelFor(model => model.IndicativoPadrao, htmlAttributes: new { @class = "control-label" })
@Html.ValidationMessageFor(model => model.IndicativoPadrao, "", new { @class = "text-danger" })
@Html.EditorFor(model => model.IndicativoPadrao, new { htmlAttributes = new { @class = "form-control", maxlength = 6 } })
</div>
<div class="form-group col-md-3 CampoForm">
@Html.LabelFor(model => model.CodigoMulta, htmlAttributes: new { @class = "control-label" })
@Html.ValidationMessageFor(model => model.CodigoMulta, "", new { @class = "text-danger" })
@Html.EditorFor(model => model.CodigoMulta, new { htmlAttributes = new { @class = "form-control", data_tipo = "numerico" } })
</div>
<div class="form-group col-md-3 CampoForm">
@Html.LabelFor(model => model.Carteira, htmlAttributes: new { @class = "control-label" })
@Html.ValidationMessageFor(model => model.Carteira, "", new { @class = "text-danger" })
@Html.EditorFor(model => model.Carteira, new { htmlAttributes = new { @class = "form-control", data_tipo = "numerico" } })
</div>
<div class="form-group col-md-3 CampoForm">
@Html.LabelFor(model => model.DiasMulta, htmlAttributes: new { @class = "control-label" })
@Html.ValidationMessageFor(model => model.DiasMulta, "", new { @class = "text-danger" })
@Html.EditorFor(model => model.DiasMulta, new { htmlAttributes = new { @class = "form-control", data_tipo = "numerico" } })
</div>
<div class="form-group col-md-3 CampoForm">
@Html.LabelFor(model => model.DiasBaixa, htmlAttributes: new { @class = "control-label" })
@Html.ValidationMessageFor(model => model.DiasBaixa, "", new { @class = "text-danger" })
@Html.EditorFor(model => model.DiasBaixa, new { htmlAttributes = new { @class = "form-control", data_tipo = "numerico" } })
</div>
<div class="form-group col-md-3 CampoForm">
@Html.LabelFor(model => model.PercentualJuros, htmlAttributes: new { @class = "control-label" })
@Html.ValidationMessageFor(model => model.PercentualJuros, "", new { @class = "text-danger" })
@Html.EditorFor(model => model.PercentualJuros, new { htmlAttributes = new { @class = "form-control", data_tipo = "decimal" } })
</div>
<div class="form-group col-md-3 CampoForm">
@Html.LabelFor(model => model.Sequencial, htmlAttributes: new { @class = "control-label" })
@Html.ValidationMessageFor(model => model.Sequencial, "", new { @class = "text-danger" })
@Html.EditorFor(model => model.Sequencial, new { htmlAttributes = new { @class = "form-control", data_tipo = "numerico" } })
</div>
<div class="form-group col-md-3 CampoForm">
@Html.LabelFor(model => model.SequencialDDA, htmlAttributes: new { @class = "control-label" })
@Html.ValidationMessageFor(model => model.SequencialDDA, "", new { @class = "text-danger" })
@Html.EditorFor(model => model.SequencialDDA, new { htmlAttributes = new { @class = "form-control", data_tipo = "numerico" } })
</div>
<div class="form-group col-md-3 CampoForm">
@Html.LabelFor(model => model.SequencialSIACC, htmlAttributes: new { @class = "control-label" })
@Html.ValidationMessageFor(model => model.SequencialSIACC, "", new { @class = "text-danger" })
@Html.EditorFor(model => model.SequencialSIACC, new { htmlAttributes = new { @class = "form-control", data_tipo = "numerico" } })
</div>
<div class="form-group col-md-3 CampoForm">
@Html.LabelFor(model => model.Operacao, htmlAttributes: new { @class = "control-label" })
@Html.ValidationMessageFor(model => model.Operacao, "", new { @class = "text-danger" })
@Html.EditorFor(model => model.Operacao, new { htmlAttributes = new { @class = "form-control", maxlength = 4 } })
</div>
<div class="form-group col-md-3 CampoForm">
<label class="form-label">Cadastro</label>
<input id="Cadastro" name="Cadastro" type="text" readonly class="form-control" value="@Cadastro" />
</div>
<div class="form-group col-md-3 CampoForm">
@Html.LabelFor(model => model.Situacao, htmlAttributes: new { @class = "control-label" })
@Html.ValidationMessageFor(model => model.Situacao, "", new { @class = "text-danger", maxlength = 1 })
@Html.DropDownListFor(model => model.EmpresaID, (List<SelectListItem>)lSituacao, " - SELECIONE - ", new { @class = "form-control" })
</div>
</div>
<div class="panel-footer">
<input type="submit" value="@ViewBag.Label" class="btn btn-primary"> | @Html.ActionLink("Cancelar", "../Administrativo/Contas/", new { id = "", acao = "" }, new { @class = "btn btn-primary" })
</div>
</div>
}
</div>
控制器
public ActionResult Contas(int pagina = 1, int EmID = 0, int FiID = 0, string Conta = "", string Situacao = "")
{
bool IsLogado = (Session["IsLogado"] is null);
if (IsLogado)
{
return RedirectToAction("Login", "User", new { acao = "ErroSessao" });
};
string Area = Session["Area"].ToString();
int UserID = Convert.ToInt32(Session["UserID"]);
int EmpresaID = Convert.ToInt32(Session["EmpresaID"]);
int FilialID = Convert.ToInt32(Session["FilialID"]);
int referenciaID = PageList.GetReferenciaID(Area, FilialID, EmpresaID, 0, 0);
referenciaID = (Area == "MAS") ? UserID : referenciaID;
EmID = (EmID == 0) ? EmpresaID : EmID;
FiID = (FiID == 0) ? FilialID : FiID;
ViewBag.EmID = EmID;
ViewBag.FiID = FiID;
ViewBag.Conta = Conta;
ViewBag.Situacao = Situacao;
//Definindo a paginação
int paginaQdteRegistros = (Session["RegistrosPorPagina"] == null) ? 10 : Convert.ToInt32(Session["RegistrosPorPagina"]);
int paginaNumeroNavegacao = pagina;
IPagedList Contas = PageList.GetAdmContas(db, paginaNumeroNavegacao, paginaQdteRegistros, Area, EmID, FiID, Conta, Situacao);
return View(Contas);
}
public ActionResult FormContas(int id = 0, string acao = "")
{
bool IsLogado = (Session["IsLogado"] is null);
if (IsLogado)
{
return RedirectToAction("Login", "User", new { acao = "ErroSessao" });
};
ViewBag.Id = id;
ViewBag.Acao = acao;
ViewBag.Label = "Cadastrar";
if (acao == "Editar") { ViewBag.Label = "Atualizar"; };
if (id == 0)
{
ViewBag.EmID = 0;
ViewBag.FiID = 0;
return View();
}
Cobrancas_Contas CobContas = DBCtx.Cob_ContasDB.Find(id);
ViewBag.EmID = CobContas.Filiais.EmpresaID;
if (CobContas == null)
{
return View();
}
return View(CobContas);
}
[Route("Administrativo/Contas/Cadastrar/")]
public ActionResult CadastrarContas(Cobrancas_Contas CobContas)
{
bool IsLogado = (Session["IsLogado"] is null);
if (IsLogado)
{
return RedirectToAction("Login", "User", new { acao = "ErroSessao" });
};
if (!ModelState.IsValid)
{
return View("FormContas", CobContas);
}
int ContaID = CobContas.ContaID;
if (ContaID == 0)
{
DBCtx.Cob_ContasDB.Add(CobContas);
}
else
{
DBCtx.Entry(CobContas).State = EntityState.Modified;
}
DBCtx.SaveChanges();
ViewModelSucesso ViewMS = new ViewModelSucesso();
ViewMS.Controller = "Administrativo";
ViewMS.View = "Contas";
return View("Sucesso", ViewMS);
}
路由配置
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapRoute(
name: "AdmContasFRM",
url: "Administrativo/Contas/Form/{id}",
defaults: new { controller = "Empresas", action = "FormContas", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "AdmContasCadastrar",
url: "Administrativo/Contas/Cadastrar/",
defaults: new { controller = "Empresas", action = "CadastrarContas"}
);
routes.MapRoute(
name: "AdmContas",
url: "Administrativo/{action}/{id}",
defaults: new { controller = "Empresas", action = "Contas", id = UrlParameter.Optional, busca = UrlParameter.Optional, vencDe = UrlParameter.Optional, vencAte = UrlParameter.Optional, pagina = UrlParameter.Optional }
);
}
}
问题是路由冲突,在我的配置中我有这个:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapRoute(
name: "AdmIndicesFRM",
url: "Administrativo/Indices/Form/{id}",
defaults: new { controller = "Empresas", action = "FormIndices", id = UrlParameter.Optional}
);
routes.MapRoute(
name: "AdmIndicesCadastrar",
url: "Administrativo/Indices/Cadastrar/",
defaults: new { controller = "Empresas", action = "CadastrarIndices" }
);
routes.MapRoute(
name: "AdmIndices",
url: "Administrativo/{action}/{id}",
defaults: new { controller = "Empresas", action = "Indices", id = UrlParameter.Optional, busca = UrlParameter.Optional, vencDe = UrlParameter.Optional, vencAte = UrlParameter.Optional, pagina = UrlParameter.Optional }
);
routes.MapRoute(
name: "AdmContasFRM",
url: "Administrativo/Contas/Form/{id}",
defaults: new { controller = "Empresas", action = "FormContas", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "AdmContasCadastrar",
url: "Administrativo/Contas/Cadastrar/",
defaults: new { controller = "Empresas", action = "CadastrarContas"}
);
routes.MapRoute(
name: "AdmContas",
url: "Administrativo/{action}/{id}",
defaults: new { controller = "Empresas", action = "Contas", id = UrlParameter.Optional, busca = UrlParameter.Optional, vencDe = UrlParameter.Optional, vencAte = UrlParameter.Optional, pagina = UrlParameter.Optional }
);
}
}
我把声明顺序改成这样,我的问题就解决了
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapRoute(
name: "AdmIndicesFRM",
url: "Administrativo/Indices/Form/{id}",
defaults: new { controller = "Empresas", action = "FormIndices", id = UrlParameter.Optional}
);
routes.MapRoute(
name: "AdmIndicesCadastrar",
url: "Administrativo/Indices/Cadastrar/",
defaults: new { controller = "Empresas", action = "CadastrarIndices" }
);
routes.MapRoute(
name: "AdmContasFRM",
url: "Administrativo/Contas/Form/{id}",
defaults: new { controller = "Empresas", action = "FormContas", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "AdmContasCadastrar",
url: "Administrativo/Contas/Cadastrar/",
defaults: new { controller = "Empresas", action = "CadastrarContas"}
);
routes.MapRoute(
name: "AdmContas",
url: "Administrativo/Contas/{id}",
defaults: new { controller = "Empresas", action = "Contas", id = UrlParameter.Optional, busca = UrlParameter.Optional, vencDe = UrlParameter.Optional, vencAte = UrlParameter.Optional, pagina = UrlParameter.Optional }
);
routes.MapRoute(
name: "AdmIndices",
url: "Administrativo/{action}/{id}",
defaults: new { controller = "Empresas", action = "Indices", id = UrlParameter.Optional, busca = UrlParameter.Optional, vencDe = UrlParameter.Optional, vencAte = UrlParameter.Optional, pagina = UrlParameter.Optional }
);
}
}