I don´t know if i understand well your problem.

If so, my suggestion is:

-> Do a projection of your model, converting it to SelectListItem List, and
pass into DropDownList Html Helper:

Example 1 - random data:
@Html.DropDownList("SelectedSiteId", new List<SelectListItem>() { new
SelectListItem() {Value = "1", Text = "Site 1"} })

Example 2:

Step 1: Pass data from controller to view:
-> Way 1: ViewBag.DropDownData = new List<Site>(); // fill data
-> Way 2: Strongly typed view.

Step 2: Doing the projection:

@Html.DropDownList("SelectedSiteId", (ViewBag.DropDownData as
List<Site>).Select(sel => new SelectListItem() { Value = sel.ID, Text =
sel.Title  }).ToList())




2015-03-31 17:10 GMT-03:00 Gunnar Liljas <[email protected]>:

> I'm sure it's possible to get model binding to work directly with the
> entities, but the recommended and proven solution is to use DTO/ViewModel
> classes instead, and handle the assignment in code, e.g with AutoMapper.
>
> /G
>
> 2015-03-31 21:12 GMT+02:00 Atilla İlhan KARTAL <
> [email protected]>:
>
>> Dear All;
>>
>> Is there someone that has got idea?
>>
>> Best Regards
>>
>> 2015-03-30 22:48 GMT+03:00 Atilla İlhan KARTAL <
>> [email protected]>:
>>
>>> Dear All;
>>>
>>> I am newbie at MVC 5. I am using Nhibernate, FluentValidation and MVC 5.
>>> I have a problem with validation. I think maybe this group users know
>>> solution.
>>>
>>> I want to use Complex Object type. I binded Site Property a dropdown
>>> Page model. I loaded Page form and post data. My model Site property has ID
>>> (ValueMember) value and Domain (DisplayMember) value. This is no problem I
>>> am using Nhibernate Merge method for save. FluentValidation Validate Site
>>> property and ModelState.IsValid is false. Because My Site Validator has
>>> RuleFor(x=>x.Title).NotEmpty(). ModelState Errors has this rule error.
>>>
>>> How can i solve this problem or How can i use DropDown for Relational
>>> Model?
>>>
>>> Best Regards.
>>>
>>>
>>>
>>> *Models*
>>>
>>> [Validator(typeof(PageValidator))]
>>>     public class Page : Persist<Page>
>>>     {
>>>         [Key]
>>>         public virtual long ID { get; set; }
>>>         public virtual Site Site { get; set; }
>>>         public virtual Page Parent { get; set; }
>>>         public virtual string Title { get; set; }
>>>         public virtual string Slug { get; set; }
>>> ...
>>> ..
>>> ..
>>> }
>>>
>>>
>>> [Validator(typeof(SiteValidator))]
>>>     public class Site : Persist<Site> , IHasDefaultValue
>>>     {
>>>         [Key]
>>>         public virtual long ID { get; set; }
>>>         public virtual string Title { get; set; }
>>>         public virtual string Description { get; set; }
>>>         public virtual string Keywords { get; set; }
>>>         public virtual string Domain { get; set; }
>>> ...
>>> ...
>>> ...
>>> ..
>>> }
>>>
>>> *View*
>>>             <div class="form-group">
>>>                 @Html.LabelFor(x => x.Site, new {@class = "col-sm-3
>>> control-label"})
>>>                 <div class="col-sm-6">
>>>                     @Html.DropDownListFor(x => x.Site.ID,
>>>                     new SelectList(Site.List(), "ID", "Domain")
>>>                     , new { @class = "form-control" })
>>>                     @Html.ValidationMessageFor(x => x.Site, null, new
>>> {@class = "help-inline error"})
>>>                 </div>
>>>             </div>
>>>
>>> *Controller*
>>>
>>> [HttpPost]
>>>         [ValidateAntiForgeryToken]
>>>         public ActionResult Create(Page model, string command)
>>>         {
>>>             if (ModelState.IsValid) /// this always false because Site
>>> property has only ID and Domain property value. Validators Validate Site
>>> property
>>>             {
>>>                 try
>>>                 {
>>>                     BeforeCreate(model);
>>>                     model.Save();
>>>                     AfterCreate(model);
>>>                     if (command=="SaveAndStay")
>>>                     {
>>>                         return RedirectToAction("Update", new { id =
>>> model.GetId() });
>>>                     }
>>>                     else if (command == "SaveAndNew")
>>>                     {
>>>                         return RedirectToAction("Create");
>>>                     }
>>>                     else
>>>                     {
>>>                         return RedirectToAction("Index");
>>>                     }
>>>                 }
>>>                 catch (Exception ex)
>>>                 {
>>>                     ModelState.AddModelError("", ex.Message);
>>>                 }
>>>             }
>>>             return View("Create", model);
>>>         }
>>>
>>> --
>>> Atilla İlhan KARTAL
>>> Web Application & Software Architect
>>> (Java & PHP & Registered Android Developer)
>>> Kuşadası / Aydın / Turkey
>>> www.atillailhankartal.com.tr
>>> twitter.com/TrojanMyth
>>>
>>
>>
>>
>> --
>> Atilla İlhan KARTAL
>> Web Application & Software Architect
>> (Java & PHP & Registered Android Developer)
>> Kuşadası / Aydın / Turkey
>> www.atillailhankartal.com.tr
>> twitter.com/TrojanMyth
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "nhusers" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to [email protected].
>> To post to this group, send email to [email protected].
>> Visit this group at http://groups.google.com/group/nhusers.
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>  --
> You received this message because you are subscribed to the Google Groups
> "nhusers" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/nhusers.
> For more options, visit https://groups.google.com/d/optout.
>



-- 

Atenciosamente,

*Gabriel RB*
*Analista de Sistemas*
TecSharp Soluções
E-mail: [email protected]
Fone: (64) 9251-9239 (operadora claro)
http://gabrielrb.net

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/nhusers.
For more options, visit https://groups.google.com/d/optout.

Reply via email to