asp.net – MVC 3在IEnumerable模型视图中编辑数据
发布时间:2020-05-24 12:04:15 所属栏目:asp.Net 来源:互联网
导读:我正在尝试在强类型剃刀视图中编辑项目列表.模板不允许我在单个视图中编辑对象列表,因此我将List视图与Edit视图合并.我只需要在复选框中编辑一个布尔字段. 问题是我无法将数据恢复到控制器.我该怎么做?的FormCollection?可视数据?提前致谢. 这是代码: 楷
|
我正在尝试在强类型剃刀视图中编辑项目列表.模板不允许我在单个视图中编辑对象列表,因此我将List视图与Edit视图合并.我只需要在复选框中编辑一个布尔字段.
这是代码: 楷模: public class Permissao
{
public int ID { get; set; }
public TipoPermissao TipoP { get; set; }
public bool HasPermissao { get; set; }
public string UtilizadorID { get; set; }
}
public class TipoPermissao
{
public int ID { get; set; }
public string Nome { get; set; }
public string Descricao { get; set; }
public int IndID { get; set; }
}
控制器动作: public ActionResult EditPermissoes(string id)
{
return View(db.Permissoes.Include("TipoP").Where(p => p.UtilizadorID == id));
}
[HttpPost]
public ActionResult EditPermissoes(FormCollection collection)
{
//TODO: Get data from view
return RedirectToAction("GerirUtilizadores");
}
视图: @model IEnumerable<MIQ.Models.Permissao>
@{
ViewBag.Title = "EditPermissoes";
}
@using (Html.BeginForm())
{
<table>
<tr>
<th></th>
<th>
Indicador
</th>
<th>
Nome
</th>
<th>Descrio</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.CheckBoxFor(modelItem => item.HasPermissao)
</td>
<td>
@Html.DisplayFor(modelItem => item.TipoP.IndID)
</td>
<td>
@Html.DisplayFor(modelItem => item.TipoP.Nome)
</td>
<td>
@Html.DisplayFor(modelItem => item.TipoP.Descricao)
</td>
</tr>
}
</table>
<p>
<input type="submit" value="Guardar" />
</p>
}
解决方法
以上都不是,使用视图模型: [HttpPost]
public ActionResult EditPermissoes(IEnumerable<Permissao> model)
{
// loop through the model and for each item .HasPermissao will contain what you need
}
在视图内部而不是编写一些循环使用编辑器模板: <table>
<tr>
<th></th>
<th>
Indicador
</th>
<th>
Nome
</th>
<th>Descrio</th>
<th></th>
</tr>
@Html.EditorForModel()
</table>
并在相应的编辑器模板内(/ Views / Shared / EditorTemplates / Permissao.cshtml): @model Permissao
<tr>
<td>
@Html.CheckBoxFor(x => x.HasPermissao)
</td>
<td>
@Html.DisplayFor(x => x.TipoP.IndID)
</td>
<td>
@Html.DisplayFor(x => x.TipoP.Nome)
</td>
<td>
@Html.DisplayFor(x => x.TipoP.Descricao)
</td>
</tr> (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- entity-framework – 如何升级EF Core Tools
- 如何在ASP.NET中动态生成列表项到无序列表?
- asp.net-mvc – ASP.Net MVC捆绑和分类
- asp.net – ASP .Net文件上载超出最大请求长度错误
- .net – 将数据写入App_Data
- ASP.NET Core 2.0 WebApi全局配置及日志实例
- asp.net – 如何从日历控件中获取所选日期?
- asp.net – WebForm_DoCallback定义
- asp.net – SQL Server查询从ADO.NET运行速度比SSMS慢
- asp.net – MVC3 WebImage助手:resize将透明背景转换为黑色
推荐文章
站长推荐
- ASP.NET UpdatePanel库引用错误
- asp.net-web-api – 如何告诉RavenDB忽略属性而不
- 在ASP.NET中开发SharePoint Web部件
- asp.net-mvc – 如何调试此错误:’无法找到iise
- ASP.NET MVC 2应用程序中的区域可以映射到子域吗
- asp.net-mvc – 错误:无法在LINQ to Entities查
- asp.net-mvc-3 – MVC HttpPostedFileBase总是空
- 有人可以向我介绍asp.net路由语法吗?
- asp.net-mvc-3 – 如何在ASP.NET MVC3控制器中访
- asp.net – 如何为Katana/Owin自主托管应用程序设
热点阅读
