ASP.Net MVC4绑定[建立查看"到包含列表中选择模型 [英] ASP.Net MVC4 bind a "create view" to a model that contains List

查看:85
本文介绍了ASP.Net MVC4绑定[建立查看"到包含列表中选择模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好在互联网土地在那里,我对你有一个有趣的难题:

Hello out there in internet land, I have an interesting conundrum for you:

是否有可能一个视图绑定用于创建对象,如果对象包含单纯使用MVC意见/局部视图等对象的列表?

Is it possible to bind a view for creating an object, if that object contains a list of other objects purely using MVC views/partial views?

男人,说出来一切复杂的东西......让我给你我的意思快速code例如:

Man, that came out all complicated like...let me give you a quick code example of what I mean:

Models:
public class ComplexObject
{
    public string title { get; set; }
    public List<ContainedObject> contents { get; set; }
}

public class ContainedObject
{
    public string name { get; set; }
    public string data { get; set; }
}

尼斯和简单吧?好了,创造的其中一个强类型视图是标题属性很简单:

Nice and simple right? Okay, so a strongly typed view for creating one of these is really simple for the "title" property:

something like:
@Html.TextBoxFor(x => x.title)

但我想不出绑定ContainedObjects使用MVC列表的好方法。我得到的最接近是创建列表脚手架模板的强类型的IEnumerable局部视图,包括在页面上。

but I can't figure out a good way to bind a list of "ContainedObjects" using MVC. The closest that I got was to create a strongly-typed IEnumerable partial view with the "List" scaffold template and include that on the page.

无需添加造型等,这部分观点的默认外观是:

Without adding styling etc, the default look of that partial view is:

@model IEnumerable<MVCComplexObjects.Models.ContainedObject>

<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.data)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.data)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
            @Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
            @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
        </td>
    </tr> 
}

</table>

但坦率地说,我无法弄清楚如何包括作为绑定到创建一个新的ComplexObject。换句话说,我可以通过如此结合表明已存在ContainedObjects的列表:
    @ Html.Partial(PartialCreate,Model.contents)

But frankly, I can't figure out how to include that as bound to the creation of a new ComplexObject. In other words, I can show a list of already existing ContainedObjects by binding as so: @Html.Partial("PartialCreate", Model.contents)

但我真的希望我猜是这样的:

But what I really want I guess, is something like:

@Html.PartialFor("PartialCreate", x => x.contents)

我要指出,我没有太多的麻烦解决此编码使用Javascript(我将在下面包括了code),但我真的很想知道,如果有一种方法与MVC纯粹做。我是从WebForms的(这里我倒是pretty简单,只是更换了所有我回发的AJAX调用反正)和这样的事情出现了很多在我工作的项目。最近改

I should note that I didn't have too much trouble coding around this with Javascript (I'll include the code below) but I'd really like to know if there's a way to do this purely with MVC. I'm a recent convert from WebForms (where I'd pretty much just replaced all of my postbacks with AJAX calls anyway) and this sort of thing comes up a lot in projects that I work on.

总之,这里是我当前如何做到这一点:

Anyway, here's how I currently do it:

HTML -

Name: <input type="text" id="enterName" />
Data: <input type="text" id="enterData" />
<a id="addItem">Add Item</a>

<ul id="addedItems">
</ul>

<a id="saveAll">Save Complex Object</a>

使用Javascript -

Javascript -

<script>
var contents = [];
$(document).ready(function () {

    $('#addItem').click(function () {
        var newItem = { name: $('#enterName').val(), data: $('#enterData').val() };
        contents.push(newItem);
        $('#addedItems').html('');
        for (var i = 0; i < contents.length; i++) {
            $('#addedItems').append(
                "<li>" + contents[i].name + ", " + contents[i].data + "</li>"
            );
        }
    });

    $('#saveAll').click(function () {

        var toPost = { title: "someTitle", contents: contents };

        $.ajax({
                url: '/Home/SaveNew',
                type: 'POST',
                data: JSON.stringify(toPost),
                dataType: 'json',
                contentType: 'application/json; charset=utf-8',
                success: function (data, textStatus, jqXHR) {
                    alert("win");
                },
                error: function (objAJAXRequest, strError) {
                    alert("fail");
                }
            });
    });

});
</script>

这不是一个可怕的解决方案或任何东西,我只是不希望有实现的Javascript每次我要保存一个新的对象通话,但使用标准的Razr code其他任何地方。我想全线相当一致。

And that's not a terrible solution or anything, I just don't want to have to implement Javascript calls everytime I want to save a new object, but use standard Razr code everywhere else. I'd like to be reasonably consistent across the board.

有没有其他人遇到了这个问题,并找到了解决办法?

Has anyone else run into this issue and found a solution?

推荐答案

我最近发现自己需要完成同样的任务,就像你,不想加了一堆的JavaScript。我使用MVC4,并尽我所知,似乎没有要到一个模型中枚举的属性绑定到一个视图一个不折不扣的即装即用的方式。 (

I recently found myself needing to accomplish the same task and, like you, not wanting to add a bunch of javascript. I'm using MVC4 and, as best I can tell, there doesn't appear to be an out-of-the-box way to bind enumerable properties of a model to a view. :(

然而,正如你在你的问题表现出来,可以可在一个视图模型检索枚举的属性。诀窍是刚刚更新到控制器。去了你的模型示例,你的看法可能是这样的(你不需要做部分):

However, as you demonstrated in your question, it is possible to retrieve enumerable properties from the model in a view. The trick is just getting the updates back to the controller. Going off of your example models, your view could look like this (you don't need to make a partial):

@model MVCComplexObjects.Models.ComplexObject

<p>
@Html.ActionLink("Create New", "Create")
</p>

@using (Html.BeginForm("SaveNew", "Home", FormMethod.Post))
{
    <table>

        <tr>
            <th>
                @Html.DisplayNameFor(model => model.contents[0].name)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.contents[0].data)
            </th>
            <th></th>
        </tr>

        @for (int i = 0; i < Model.contents.Count; i++)
        {
            <tr>
                <td>
                    @Html.TextBox("updatedContents["+i+"].name", Model.contents[i].name)
                </td>
                <td>
                    @Html.TextBox("updatedContents["+i+"].data", Model.contents[i].data)
                </td>
                <td>
                    @* Got rid of the edit and detail links here because this form can now act as both *@
                    @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
                </td>
            </tr> 
        }

    </table>

    <input type="submit" value="Save" />
}

和控制器的动作应该是这样的:

And your controller action would look like this:

[HttpPost]
public ActionResult SaveNew(ICollection<ContainedObject> updatedContents)
{
    foreach (var co in updatedContents)
    {
        //Update the contained object...
    }

    return RedirectToAction("Index");
}

基本上,我们都在为MVC视图定义一个新的集合对象传递给提交表单的操作方法。新的对象(本例中updatedContents)是基本上相同的定义和在的把complexObject模型填充列表属性(内容,在本例中)。

Basically, we are defining a new collection object in the view for MVC to pass to your action method upon form submission. The new object ("updatedContents" in this example) is basically the same as the list property ("contents", in this example) that was defined and populated in the ComplexObject model.

这是多一点的工作,但并没有完成对需要后任何JavaScript回的目标。一切都可以用标准的MVC实现。

This is a bit more work, but does accomplish the goal of not needing any javascript for the post back. Everything can be done with standard MVC.

这篇关于ASP.Net MVC4绑定[建立查看&QUOT;到包含列表中选择模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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