开发者

Retrieve Selected Item from Dropdownlist In MVC 3 Razor

I have a list in the controller and my code looks like this.

ViewBag.Org开发者_JAVA百科anizations = _frontendUserService.GetOrganizationByClientId(Constants.ClientId);

And I'm setting this list in my View Page with a Dropdownlist.

@Html.DropDownListFor(
    model => model.Organization.OrganizationId, 
    new SelectList(
        ViewBag.Organizations as System.Collections.IEnumerable, 
        "OrganizationId", 
        "OrganizationName"),
    "-- Select Organization --")

Here I get the OrganizationId as the selected item. Instead I want to retrieve the selected item as an "Organization" object in the POST request to my action method.

Actually why I wanted to get the entire object here is, because when I'm querying the database by OrganizationId I'm getting following error.

Error: sequence contains more than one matching element

In my application I'm using repository pattern.


It could be done, but I don't see the point in it. It's most likely a lot more efficient to fetch it from the database again instead of trying to do what you are asking.


I was trying to do something similar, but struggled - ended up using some of the code of this site and the generator tool, works quite well: http://www.mvc3razor.com/sample-code/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜