C# 不使用我指定的ActionName的部分视图
我目前正在用ASP.NETMVC和实体框架开发一个C#应用程序 我使用partialview编辑数据,例如,我有一个表格形式的“模块”列表,我可以添加模块、删除模块或编辑模块 要创建模块,请按以下步骤进行操作: 我的看法是:C# 不使用我指定的ActionName的部分视图,c#,asp.net,asp.net-mvc,entity-framework-6,C#,Asp.net,Asp.net Mvc,Entity Framework 6,我目前正在用ASP.NETMVC和实体框架开发一个C#应用程序 我使用partialview编辑数据,例如,我有一个表格形式的“模块”列表,我可以添加模块、删除模块或编辑模块 要创建模块,请按以下步骤进行操作: 我的看法是: <asp:UpdatePanel ID="ModuleUpdatePanel" runat="server"> <ContentTemplate> <!-- Modal --> <asp:Label I
<asp:UpdatePanel ID="ModuleUpdatePanel" runat="server">
<ContentTemplate>
<!-- Modal -->
<asp:Label ID="Label1" runat="server" Text=""></asp:Label><br />
<div class="modal fade" id="ModuleModal" tabindex="-1" role="dialog" aria-labelledby="ModuleModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
@Html.Action("_CreateModule")
</div>
</div>
</div>
</ContentTemplate>
</asp:UpdatePanel>
部分观点:
@using (Html.BeginForm("CreateModule", "Parametres", FormMethod.Post))
{
@model Lynx.ViewModels.ModuleViewModel
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<h4 class="modal-title" id="ModuleModalLabel">
Ajouter un module
</h4>
</div>
<div class="modal-body">
<div class="form-group">
@Html.TextBoxFor(m => m.Module.Libelle_Module, new { @class = "form-control", @placeholder = "Libellé du module" })
@Html.ValidationMessageFor(m => m.Module.Libelle_Module)
</div>
<div class="form-group">
@Html.TextBoxFor(m => m.Module.Capacite_Max_Module, new { @class = "form-control", @placeholder = "Capacité max (Palettes/heure)" })
@Html.ValidationMessageFor(m => m.Module.Capacite_Max_Module)
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">
Annuler
</button>
<input type="submit" class="btn btn-success" value="Enregistrer" />
</div>
}
对于这一部分,一切正常。我遇到问题的地方是我的模块版本(我使用相同的过程)
视图:
<div class="modal-content">
@Html.Action("_EditModule", "Parametres", new { id = @module.PK_Code_Module })
</div>
部分观点:
@using (Html.BeginForm("EditModule", "Parametres", FormMethod.Post))
{
@model Lynx.Models.Modules
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<h4 class="modal-title" id="EditModuleModalLabel">
Modifier un module
</h4>
</div>
<div class="modal-body">
<div>
<b>Id Module</b>
@Html.TextBoxFor(model => model.PK_Code_Module, new { @class = "form-control", @readonly = "readonly" })
</div>
<div>
<b>Libellé</b>
@Html.TextBoxFor(model => model.Libelle_Module, new { @class = "form-control" })
</div>
<div>
<b>Capacité max du module (colis/heure)</b>
@Html.TextBoxFor(model => model.Capacite_Max_Module, new { @class = "form-control" })
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">
Annuler
</button>
<input type="submit" class="btn btn-success" value="Enregistrer" />
</div>
}
这里我的按钮“Enregistrer”完全不起作用。。。好像他忽略了我的“通知”
有什么想法吗?将[HttpPost]放入控制器上的操作中。就像这样:
[HttpPost]
[Authorize]
[ActionName("EditModule")]
public void EditModule(Modules module)
{
if (Dal.GetModule(module.PK_Code_Module) != null)
{
Dal.UpdateModule(int.Parse(module.PK_Code_Module), module.Libelle_Module, module.Capacite_Max_Module);
}
String URL = HttpContext.Request.Url.AbsoluteUri;
string[] lines = Regex.Split(URL, "/");
if (lines[lines.Length - 1] == "Parametres")
{
Response.Redirect("Parametres");
}
else
{
Response.Redirect("Index");
}
}
谢谢,它很管用!我只是对数组的第一个元素有一个问题(它仍然不起任何作用),我尝试了其他方法,在调用我的模式的按钮周围添加了@using html.beginform。它可以在我所有的记录上运行,没有[HttpPost]标记。听起来有点可疑,但它起作用了。。。但这似乎有点可疑
[Authorize]
public ActionResult _EditModule(int Id)
{
Modules module = Dal.GetModule(Id);
return PartialView(module);
}
@using (Html.BeginForm("EditModule", "Parametres", FormMethod.Post))
{
@model Lynx.Models.Modules
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<h4 class="modal-title" id="EditModuleModalLabel">
Modifier un module
</h4>
</div>
<div class="modal-body">
<div>
<b>Id Module</b>
@Html.TextBoxFor(model => model.PK_Code_Module, new { @class = "form-control", @readonly = "readonly" })
</div>
<div>
<b>Libellé</b>
@Html.TextBoxFor(model => model.Libelle_Module, new { @class = "form-control" })
</div>
<div>
<b>Capacité max du module (colis/heure)</b>
@Html.TextBoxFor(model => model.Capacite_Max_Module, new { @class = "form-control" })
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">
Annuler
</button>
<input type="submit" class="btn btn-success" value="Enregistrer" />
</div>
}
[Authorize]
[ActionName("EditModule")]
public void EditModule(Modules module)
{
if (Dal.GetModule(module.PK_Code_Module) != null)
{
Dal.UpdateModule(int.Parse(module.PK_Code_Module), module.Libelle_Module, module.Capacite_Max_Module);
}
String URL = HttpContext.Request.Url.AbsoluteUri;
string[] lines = Regex.Split(URL, "/");
if (lines[lines.Length - 1] == "Parametres")
{
Response.Redirect("Parametres");
}
else
{
Response.Redirect("Index");
}
}
[HttpPost]
[Authorize]
[ActionName("EditModule")]
public void EditModule(Modules module)
{
if (Dal.GetModule(module.PK_Code_Module) != null)
{
Dal.UpdateModule(int.Parse(module.PK_Code_Module), module.Libelle_Module, module.Capacite_Max_Module);
}
String URL = HttpContext.Request.Url.AbsoluteUri;
string[] lines = Regex.Split(URL, "/");
if (lines[lines.Length - 1] == "Parametres")
{
Response.Redirect("Parametres");
}
else
{
Response.Redirect("Index");
}
}