如何从视图中的MVC 3返回列表或集合到控制器? [英] How do I return List or Collection to Controller from View in MVC 3?

查看:82
本文介绍了如何从视图中的MVC 3返回列表或集合到控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人请帮我,从我的观点正确返回此列表。我不明白为什么我返回null我fieldModelList我试图传递给控制器​​...

下面是我的看法:

  @model名单< Regions.SOA.UI.CopyBookSchemaCreator.Models.FieldModel><脚本类型=文/ JavaScript的SRC =〜/脚本/ jQuery的-UI-1.8.11.min.js>< / SCRIPT>@using(Html.BeginForm(GetResponse的,TestMethods,FormMethod.Post))
{<表ID =tblMethods>
    &所述; TR>
        <第i个
            物业名称
        < /第i
        <第i个
            请求
        < /第i
    < / TR>    @foreach(FieldModel fieldModel的模型)
    {
        &所述; TR>
            &所述; TD>
                @ Html.DisplayFor(M = GT; fieldModel.PropertyName)
            < / TD>
            &所述; TD>
                @ Html.TextBoxFor(M = GT; fieldModel.PropertyValue)
            < / TD>
        < / TR>
    }< /表>< D​​IV>
    <输入类型=提交/>
< / DIV>

和这里是我的控制器:

  [HttpPost]
    公众的ActionResult的GetResponse(列表< FieldModel> fieldModelList)
    {
        返回的GetResponse(fieldModelList);
    }

我打的HttpPost方法,但如果我把一个断点,只是里面,我返回null为fieldModelList马上蝙蝠,我希望会是我进入texboxes视图上的值的列表这是模型的FieldModel ...

我觉得有什么不对我的逻辑与我的语法,或者也许还有我的语法,但基本上是我想要做的就是回到背式FieldModel的列表,每个相应的属性名,并为PropertyValue到控制器。我注意到,我没有经过任何形式的id参数在我BeginForm声明中的观点。我需要一个在这里?

以防万一,这里是我的FieldModel模型类:

 命名空间Regions.SOA.UI.CopyBookSchemaCreator.Models
{
    公共类FieldModel
    {
        [显示(名称=属性)]
        公共字符串属性名{获得;组; }    [显示(名称=值)
        公共字符串为PropertyValue {搞定;组; }
    }
}


解决方案

菲尔哈克的写了一篇文章前段时间解释如何绑定集合(的ICollection )来查看型号。它会进入更多的细节有关创建编辑模板,你当然可以做的一样好。

基本上,你需要preFIX HTML元素的名字与索引属性。

 <输入类型=文本名称=[0] .PropertyNameVALUE =好奇的乔治/>
<输入类型=文本名称=[0] .PropertyValueVALUE =。H.A雷伊/><输入类型=文本名称=[1] .PropertyNameVALUE =安德的游戏/>
<输入类型=文本名称=[1] .PropertyValueVALUE =奥森斯科特卡德/>

然后,您的控制器可以结合 FieldModel

集合

  [HttpPost]
公众的ActionResult的GetResponse(列表< FieldModel> fieldModelList)
{
    返回的GetResponse(fieldModelList);
}

我不是100%肯定下将正确命名的属性(我推荐使用编辑模板),但你可以很容易地使用 htmlAttributes 参数,并给它使用索引的名称。

  @for(INT I = 0; I< Model.Count;我++)
{
    &所述; TR>
        &所述; TD>
            @ Html.DisplayFor(M = GT; M [​​] .PropertyName)
        < / TD>
        &所述; TD>
            @ Html.TextBoxFor(M = GT; M [​​] .PropertyValue)
        < / TD>
    < / TR>
}

编辑模板

如果你想尽可能增加一个编辑模板去,添加一个名为局部视图 FieldModel.ascx /查看/共享是强类型的一个 FieldModel

  @model Regions.SOA.UI.CopyBookSchemaCreator.Models.FieldModel@ Html.TextBoxFor(M => m.PropertyName)@ *这可能是一个标签? * @
@ Html.TextBoxFor(M = GT; m.PropertyValue)

和,那么你的视图的一部分负责呈现集合会是什么样子:

  @for(INT I = 0; I< Model.Count;我++){
    @ Html.EditorFor(M = GT; M [​​]);
}

Someone please help me return this list properly from my view. I don't see why I'm returning null for my fieldModelList I try to pass to the controller...

Here is my view:

@model List<Regions.SOA.UI.CopyBookSchemaCreator.Models.FieldModel>

<script type="text/javascript" src="~/Scripts/jquery-ui-1.8.11.min.js"></script>

@using (Html.BeginForm("GetResponse", "TestMethods", FormMethod.Post))
{

<table id="tblMethods">
    <tr>
        <th>
            Property Name
        </th>
        <th>
            Request
        </th>
    </tr>

    @foreach (FieldModel fieldModel in Model) 
    {
        <tr>
            <td>
                @Html.DisplayFor(m => fieldModel.PropertyName)
            </td>
            <td>
                @Html.TextBoxFor(m => fieldModel.PropertyValue)
            </td>
        </tr>
    }

</table>

<div>
    <input type="submit"/>       
</div>

and here is my controller:

    [HttpPost]
    public ActionResult GetResponse(List<FieldModel> fieldModelList)
    {
        return GetResponse(fieldModelList);   
    }

I am hitting the HttpPost method but if I place a breakpoint just inside it, I am returning null for the fieldModelList right off the bat, which I was hoping would be a list of the values I entered into the texboxes on the view that is of model FieldModel...

I think something is wrong with my logic versus my syntax, or as maybe as well as my syntax, but basically what I want to do is return back a list of type FieldModel with each corresponding PropertyName and PropertyValue to the controller. I noticed I am not passing any kind of id parameter in my BeginForm statement in the view. Do I need one here?

Just in case, here is my model class for FieldModel:

namespace Regions.SOA.UI.CopyBookSchemaCreator.Models
{
    public class FieldModel
    {
        [Display(Name = "Property")]
        public string PropertyName { get; set; }

    [Display(Name = "Value")]
        public string PropertyValue { get; set; }
    }
}

解决方案

Phil Haack wrote an article some time ago explaining how to bind collections (ICollection) to view models. It goes into additional detail about creating an editor template, which you could certainly do as well.

Basically, you need to prefix the HTML elements' name attributes with an index.

<input type="text" name="[0].PropertyName" value="Curious George" />
<input type="text" name="[0].PropertyValue" value="H.A. Rey" />

<input type="text" name="[1].PropertyName" value="Ender's Game" />
<input type="text" name="[1].PropertyValue" value="Orson Scott Card" />

Then, your controller could bind the collection of FieldModel

[HttpPost]
public ActionResult GetResponse(List<FieldModel> fieldModelList)
{
    return GetResponse(fieldModelList);   
}

I'm not 100% sure the following would name the attributes correctly (I'd recommend using the editor template) but you could easily use the htmlAttributes argument and give it a name using the index.

@for(int i = 0;i < Model.Count;i++) 
{
    <tr>
        <td>
            @Html.DisplayFor(m => m[i].PropertyName)
        </td>
        <td>
            @Html.TextBoxFor(m => m[i].PropertyValue)
        </td>
    </tr>
}

Editor Template

If you wanted to go as far as adding an editor template, add a partial view named FieldModel.ascx to /Views/Shared that is strongly typed to a FieldModel

@model Regions.SOA.UI.CopyBookSchemaCreator.Models.FieldModel

@Html.TextBoxFor(m => m.PropertyName) @* This might be a label? *@
@Html.TextBoxFor(m => m.PropertyValue)

And, then the part of your view responsible for rendering the collection would look like:

@for (int i = 0; i < Model.Count; i++) { 
    @Html.EditorFor(m => m[i]);
}

这篇关于如何从视图中的MVC 3返回列表或集合到控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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