ASP.NET MVC2,LINQ到SQL关系的实体,选择列表,模型绑定 [英] ASP.NET MVC2, LINQ-TO-SQL relationship entities, select list, model binding

查看:98
本文介绍了ASP.NET MVC2,LINQ到SQL关系的实体,选择列表,模型绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设你有以下情况:


  • 表:国家;列:CountryID,国家或地区名称

  • 表:国;列:STATEID,Statename的,CountryID(外键)

  • 表:地址;列:LocalityID,处所名称,STATEID(外键)

在MVC中,它是很好,在code自动生成的LINQ到SQL类映射关联关系,所以你可以调用链接的外国实体,例如在我看来,我可以有:

In MVC, it is nice that the auto-generated LINQ-TO-SQL classes map association relationships in the code so you can call the linked foreign entity, e.g. in my view I can have:

<% foreach (var locality in Model.Localities) { %>

    <tr>
        <td>
            <%: locality.LocalityName %>
        </td>
        <td>
            <%: locality.State.StateName %>
        </td>
        <td>
            <%: locality.State.Country.CountryName %>
        </td>
    </tr>

<% } %>

当我浏览到该索引操作这成功地呈现。

This succesfully renders when I browse to the index action.

不过,我有编辑行动问题。这里的code:

However, I am having problems with the Edit action. Here's the code:

        <strong>Locality Name</strong>
        <div class="editor-field">
            <%: Html.TextBox("LocalityName", Model.Locality.LocalityName) %>
            <%: Html.ValidationMessage("LocalityName")%>
        </div>

        <strong>Country</strong>
        <div class="editor-field">
            <%= Html.DropDownList("CountryID", Model.CountriesDDL, "-- select --") %>
        </div>

        <strong>State</strong>
        <div class="editor-field">
            <%= Html.DropDownList("StateID", Model.StatesDDL, "-- select--") %>
            <%: Html.ValidationMessage("StateID")%>
        </div>

起初我得到一个验证错误状态,上面写着:例如,选择列表(用于STATEID)旁边的8不是有效的价值。因此,它没有更新。

At first I was getting a validation error next to the selectlist of states (for the StateID) which said, e.g., "8 is not a valid value." So it isn't updating.

据我了解,从阅读一些职位(<一href=\"http://stackoverflow.com/questions/2328413/linq-to-sql-foreignkeyreferencealreadyhasvalueexception-error\">such这一个的),有一个与数据库相关的问题更新通过设置该ID的关系,并且该解决方案似乎是设置ENTITY代替。 (顺便说一句,相反的意见,也省略<一个href=\"http://stackoverflow.com/questions/662651/asp-net-mvc-model-binding-foreign-key-relationship\">here).根据这一建议,我一直在寻找Html.DropDownList()的重载来设置列表项的对象本身的价值,而文本属性设置为Object.Name - 我还没有解决了这个然而,但由于这项工作变得更加费时,我就非常AP preciate一些专家的意见。

I understand, from reading a number of posts (such as this one), that there is a database-related problem updating the relationship by setting the ID, and that the solution appears to be to set the ENTITY instead. (Incidentally, contrary advice is also dispensed here). Following this advice, I have been looking at overloads of Html.DropDownList() to set the "value" of the list item to the object itself, whilst setting the "text" property to the Object.Name - I haven't solved this yet. But as this undertaking is becoming more time-consuming, I would very much appreciate some expert advice.

所以,我的问题是:如果我得到它打算将这项实际工作?而且是有可能得到选择列表这样的工作方式(以及任何建议,如何)?

Therefore, my questions are: will this actually work if I get it going? And is it possible to get the SelectList to work this way (and any suggestions as to how)?

感谢您的时间,

添。

推荐答案

在总体上也没有你的域模型传递到您的视图是一个好主意(或许除了列表中查看)。

In general it is also not a good idea to pass your domain model to your View (except perhaps in list Views).

更好的使用(代理)视图模型这是您的域模型的扁平版本。这是一个有点更多的工作,但就像一个魅力。

Better using a (proxy) ViewModel which is a flattened version of your domain model. It is a bit more work, but works like a charm.

这篇关于ASP.NET MVC2,LINQ到SQL关系的实体,选择列表,模型绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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