MVC 3在IEnumerable的模型视图编辑数据 [英] MVC 3 Edit data in a IEnumerable Model View

查看:157
本文介绍了MVC 3在IEnumerable的模型视图编辑数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在一个强类型的Razor视图编辑的项目清单。这些模板不给我在一个单一的视图中编辑对象的列表选项,所以我合并了编辑视图列表视图。我只需要在一个复选框,编辑一个布尔领域。
问题是,我不能得到数据传回给控制器。我该怎么做? 的FormCollection 可视数据?先谢谢了。

这里的code:

型号:

 公共类Permissao
{
    公众诠释ID {搞定;组; }
    公共TipoPermissao TipoP {搞定;组; }
    公共BOOL HasPermissao {搞定;组; }
    公共字符串UtilizadorID {搞定;组; }
}公共类TipoPermissao
{
    公众诠释ID {搞定;组; }
    公共字符串诺姆{搞定;组; }
    公共字符串Descricao {搞定;组; }
    公众诠释的indid {搞定;组; }
}

控制器操作:

 公众的ActionResult EditPermissoes(字符串ID)
    {
        返回视图(db.Permissoes.Include(TipoP)如(p值=方式> p.UtilizadorID == ID));
    }    [HttpPost]
    公众的ActionResult EditPermissoes(的FormCollection集合)
    {
        // TODO:从视图数据
        返回RedirectToAction(GerirUtilizadores);
    }

查看:

  @model IEnumerable的< MIQ.Models.Permissao>@ {
    ViewBag.Title =EditPermissoes;
}@using(Html.BeginForm())
{
    <表>    &所述; TR>
        百分位>< /第i
        <第i个
            Indicador
        < /第i
        <第i个
            诺姆
        < /第i
        <第iDescrição< /第i
        百分位>< /第i
    < / TR>
    @foreach(以型号VAR项){
        &所述; 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>
    }
< /表>
&所述p为H.;
   <输入类型=提交值=和保存/>
 &所述; / P>
}


解决方案

  

我该怎么办呢?的FormCollection?可视数据?


以上,使用视图模型无:

  [HttpPost]
公众的ActionResult EditPermissoes(IEnumerable的< Permissao>模型)
{
    //遍历模型,并对每个项目.HasPermissao将包含你所需要的
}

和您的看法,而不是写一些循环里面使用的编辑器模板:

 <表>
    &所述; TR>
        百分位>< /第i
        <第i个
            Indicador
        < /第i
        <第i个
            诺姆
        < /第i
        <第iDescrição< /第i
        百分位>< /第i
    < / TR>
    @ Html.EditorForModel()
< /表>

和相应的编辑模板内(〜/查看/共享/ 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>

I'm trying to edit a list of items in a strongly-typed razor view. The templates don't give me the option to edit a list of objects in a single view so I merged the List view with the Edit view. I only need to edit one boolean field in a checkbox. The problem is that i cant get the data back to the controller. How do i do it? FormCollection? Viewdata? Thanks in advance.

Here's the code:

Models:

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

Controller Actions:

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

View:

@model IEnumerable<MIQ.Models.Permissao>

@{
    ViewBag.Title = "EditPermissoes";
}

@using (Html.BeginForm())
{
    <table>

    <tr>
        <th></th>
        <th>
            Indicador
        </th>
        <th>
            Nome
        </th>
        <th>Descrição</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>
}

解决方案

How do i do it? FormCollection? Viewdata?

None of the above, use the view model:

[HttpPost]
public ActionResult EditPermissoes(IEnumerable<Permissao> model)
{
    // loop through the model and for each item .HasPermissao will contain what you need
}

And inside your view instead of writing some loops use editor templates:

<table>
    <tr>
        <th></th>
        <th>
            Indicador
        </th>
        <th>
            Nome
        </th>
        <th>Descrição</th>
        <th></th>
    </tr>
    @Html.EditorForModel()
</table> 

and inside the corresponding editor template (~/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>

这篇关于MVC 3在IEnumerable的模型视图编辑数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆