English 中文(简体)
MVC3 致选名单的范本
原标题:MVC3 Model Binding to a Select List

我早就利用了MVSCcaf手套,为我用EF4储存的数据创建了CRDO 。 页: 1

它对基本假设情景运作良好,所有数据都与文字箱相挂钩。

然而,我希望我的“个人”的“Title”财产受到一个特定清单的约束。 我希望不要形成一种观点模式,而是坚持对实际个人模式具有约束力的观点。

public class Person    {
    public string Title { get; set; }
    .......

我认为,这是正确的,但选定项目并没有正确约束。 尽管实际数据可能有所不同,但总是将“先生”列为选定项目。

@Html.DropDownListFor(model => model.Title, new SelectList(new[] { "Mr", "Mrs", "Ms", "Miss", "Sir", "Dr", }, Model.Title))

这些工作包括:

@Html.DropDownList("Title", new SelectList(new[] { "Mr", "Mrs", "Ms", "Miss", "Sir", "Dr", }, Model.Title))

然而,这确实是:

@Html.DropDownList("TitleX", new SelectList(new[] { "Mr", "Mrs", "Ms", "Miss", "Sir", "Dr", }, Model.Title))

但是,在试图更新记录时,我得到了“客观的参考,而不是标的”例外。

我怎么能以尽量减少对我的模型或控制器的干扰来做到这一点?

问题回答

第一点应该正确。 如果所有权领域确实适用于数据库中存在的内容,则在你的控制人员返回之前,由你检查。 此外,还核对案文背后的文字。 如果在您的数据库中“Ms ”存在,则第一个项目将开放,因为名单上不存在女士。





相关问题
WebForms and ASP.NET MVC co-existence

I am trying to make a WebForms project and ASP.NET MVC per this question. One of the things I ve done to make that happen is that I added a namespaces node to the WebForms web.config: <pages ...

Post back complex object from client side

I m using ASP.NET MVC and Entity Framework. I m going to pass a complex entity to the client side and allow the user to modify it, and post it back to the controller. But I don t know how to do that ...

Create an incremental placeholder in NHaml

What I want to reach is a way to add a script and style placeholder in my master. They will include my initial site.css and jquery.js files. Each haml page or partial can then add their own required ...

asp.net mvc automapper parsing

let s say we have something like this public class Person { public string Name {get; set;} public Country Country {get; set;} } public class PersonViewModel { public Person Person {get; ...

structureMap mocks stub help

I have an BLL that does validation on user input then inserts a parent(PorEO) and then inserts children(PorBoxEO). So there are two calls to the same InsertJCDC. One like this=>InsertJCDC(fakePor)...

ASP.NET MVC: How should it work with subversion?

So, I have an asp.net mvc app that is being worked on by multiple developers in differing capacities. This is our first time working on a mvc app and my first time working with .NET. Our app does not ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

热门标签