ASP.Net MVC3模型绑定的IEnumerable< T>与编辑模板 [英] ASP.Net MVC3 Model Binding IEnumerable<T> with Editor Template

查看:311
本文介绍了ASP.Net MVC3模型绑定的IEnumerable< T>与编辑模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所有,请清除了我的困惑是如何在模型绑定与IEnumerables和编辑模板的作品。

我有一个观点,Approve.cshtml

  @model IEnumerable的< MvcWebsite.Models.Approve>
<表>
    &所述; TR>
        <第i个
            名称
        < /第i
    < / TR>
    @ Html.EditorForModel()
< /表>

一个模型,Approve.cs

 公共类批准
{
  公共字符串名称{;设置;}
  公共字符串角色{搞定;组; }
}

和编辑模板

  @model MvcWebsite.Models.Approve@using(Html.BeginForm(批准,注册,FormMethod.Post))
{
&所述; TR>
    &所述; TD>
        @ Html.HiddenFor(M = GT; m.Name)
        @ Html.EditorFor(M = GT; m.Role)
    < / TD>
    &所述; TD>
        <输入类型=提交值=批准级=提交键/>
    < / TD>
< / TR>

}

这是一切优秀和良好。它呈现以下输出。

 <输入名称=[0] .Name点类型=隐藏值=/>
        ....

不过,在我的控制,我似乎无法接收值回模型(绑定)。

  [HttpPost]
公众的ActionResult批准(批准approveModel)
{
    .... approveModel具有所有默认值
}

有人可以阐明我做错了什么在这里光?我简写code,我现在用的模板编辑从我的模型......

其他EditorFor和HiddenFor领域

编辑:我基本上是有一个表的布局,每一个用户名,一个文本框,在那里我可以进入自己的角色(用户或管理员),然后一批准按钮,提交给我的控制器。因此,我想,之所以只返回一个批准对象。我可以把整个的IEnumerable回到我的控制器,但如果我这样做,我怎么能知道哪些项目的是一个我点击了批准按钮(提交)?

编辑:
所以,我已经修改了code,让我有一个单一的形式围绕我的整个浏览Approve.cshtml

  @model IEnumerable的< MvcWebsite.Models.Approve>
@using(Html.BeginForm(批准,计划,FormMethod.Post))
{
<表>
    &所述; TR>
        <第i个
            名称
        < /第i
    < / TR>
    @ Html.EditorForModel()
< /表>
}

再变控制器

  [HttpPost]
公众的ActionResult批准(IEnumerable的<批准并GT;批准)
{
    // ???????????????????????
}

现在我还没有就如何知道我点击哪一行批准清楚。我知道有其他的方法来完成这项任务(创建批准复选框,并批准任何检查,等),但我需要点击一个按钮,只有1排回保存到数据库的能力,无论如果用户输入信息到其他行。它是更好的做法来包装它自己的模式(即AllApprovals)我的IEnumerable内,然后辅助属性添加到父模型(的SelectedIndex等)?如果是采取的方法,那么我该如何设置点击一个按钮,批准后的SelectedIndex?是仍然jQuery的魔术还是有一个正确的MVC的方式来做到这一点? jQuery的魔力似乎很hackish的给我?

编辑:基于Brian的反应,这是我的最终决定。仍然没有手感相当不错,但它的作品!

查看

  @model IEnumerable的< MvcWebsite.Models.Approve>
<表>
    &所述; TR>
        <第i个
            名称
        < /第i
    < / TR>
    @ Html.EditorForModel()
< /表>

编辑模板

  @using(Html.BeginForm(批准,注册,FormMethod.Post))
{
&所述; TR>
    &所述; TD>
        @ Html.HiddenFor(M = GT; m.Name)
        @ Html.EditorFor(M = GT; m.Role)
    < / TD>
    &所述; TD>
        <输入类型=提交值=批准级=提交键/>
    < / TD>
< / TR>
}

控制器

  [HttpPost]
公众的ActionResult批准([装订(preFIX =审批表)批准的批准){
    // WORKS!
}


解决方案

既然你一次只改变一个,我认为下面是不是试图在该值发生了变化,控制器要弄清楚,或添加更改的属性更容易并通过JavaScript设置它。

更改为Approve.cshtml

  @model IEnumerable的< MvcWebsite.Models.Approve>
<表>
    &所述; TR>
        百分位列跨度= 2 - ;
            名称
        < /第i
    < / TR>
@foreach(以型号VAR用户){
    @using(Html.BeginForm(批准,注册,FormMethod.Post)){
    &所述; TR>
        &所述; TD>
            @ Html.EditorFor(M = gt;用户)
        < / TD>
        &所述; TD>
            <输入类型=提交值=批准级=提交键/>
        < / TD>
    < / TR>
    }
}
< /表>

更改批准编辑模板,以

  @ Html.HiddenFor(M = GT; m.Name)
@ Html.EditorFor(M = GT; m.Role)

All, please clear up my confusion on how the model binding works with IEnumerables and Editor Templates.

I have a view, Approve.cshtml

@model IEnumerable<MvcWebsite.Models.Approve>
<table>
    <tr>
        <th>
            Name
        </th>
    </tr>
    @Html.EditorForModel()
</table>

A model, Approve.cs

public class Approve
{
  public string Name { get;set;}
  public string Role { get; set; }
}

And an editor template

@model MvcWebsite.Models.Approve

@using (Html.BeginForm("Approve", "Registration", FormMethod.Post))
{
<tr>
    <td>
        @Html.HiddenFor(m => m.Name)
        @Html.EditorFor(m => m.Role)
    </td>
    <td>
        <input type="submit" value="Approve" class="submit-button" />            
    </td>
</tr>

}

This is all fine and good. It renders the following output.

        <input name="[0].Name" type="hidden" value="" />
        ....

However, in my Controller, I can't seem to receive values back for the Model (binding).

[HttpPost]
public ActionResult Approve(Approve approveModel)
{
    .... approveModel has all default values
}

Can someone shed light on what I am doing wrong here? I abbreviated the code, I am using the Editor Template with other EditorFor and HiddenFor fields from my model...

Edited: I basically have a table layout, each with the User's Name, a textbox where I can enter their role (User or Admin) and then an Approve button which submits to my controller. Hence the reason I want to only return a single Approve object. I can return the entire IEnumerable to my Controller, but if I do that, how can I tell which of the items was the one I clicked the Approve button (submit) for?

EDIT: So I have modified the code so that I have a single form surrounding my entire View Approve.cshtml

@model IEnumerable<MvcWebsite.Models.Approve>
@using (Html.BeginForm("Approve", "Program", FormMethod.Post))
{
<table>
    <tr>
        <th>
            Name
        </th>
    </tr>
    @Html.EditorForModel()
</table>
}

And then changed the controller to

[HttpPost]
public ActionResult Approve(IEnumerable<Approve> approvals)
{
    // ???????????????????????
}

Now I'm still not clear on how to know which row I clicked Approve for. I know there are other ways to accomplish this task (create a checkbox for approve, and approve anything checked, etc.) However, I need the ability to click a button and only save 1 row back to the database, regardless if the user entered information into the other rows. Is it better practice to wrap my IEnumerable inside of it's own model (i.e. AllApprovals) and then add helper properties to that parent model (SelectedIndex, etc.)? If that is the approach to take, then how do I set the SelectedIndex after clicking an Approve button? Is that still jquery magic or is there a correct MVC way to accomplish this? Jquery magic seems very hackish to me?

EDIT: Based on Brian's response, here is my final. Still doesn't feel quite right, but it works!

View

@model IEnumerable<MvcWebsite.Models.Approve>
<table>
    <tr>
        <th>
            Name
        </th>
    </tr>
    @Html.EditorForModel()
</table>

Editor Template

@using (Html.BeginForm("Approve", "Registration", FormMethod.Post))
{
<tr>
    <td>
        @Html.HiddenFor(m => m.Name)
        @Html.EditorFor(m => m.Role)
    </td>
    <td>
        <input type="submit" value="Approve" class="submit-button" />            
    </td>
</tr>
}

Controller

[HttpPost]
public ActionResult Approve([Bind(Prefix="approval")]Approve approval)    {
    // WORKS!
}

解决方案

Since you are only changing one at a time, I think the following is easier than trying to figure out at the controller which values changed, or adding a changed property and setting it via javascript.

Change Approve.cshtml to

@model IEnumerable<MvcWebsite.Models.Approve> 
<table> 
    <tr> 
        <th colspan=2> 
            Name 
        </th> 
    </tr> 
@foreach(var user in Model){
    @using (Html.BeginForm("Approve", "Registration", FormMethod.Post))         { 
    <tr> 
        <td> 
            @Html.EditorFor(m => user) 
        </td> 
        <td> 
            <input type="submit" value="Approve" class="submit-button" />             
        </td> 
    </tr> 
    }
}
</table> 

Change the Approve Editor Template to

@Html.HiddenFor(m => m.Name) 
@Html.EditorFor(m => m.Role) 

这篇关于ASP.Net MVC3模型绑定的IEnumerable&LT; T&GT;与编辑模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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