c# - Potential AutoMapper issue -


I have a problem with my drop down list I have posted earlier: but things have changed, so I Resubmitting again.

I have a visual model that looks like this:

  Public class countryview model {get public int country id { Set; } Public string text {get; Set; } Public IEnumerable & lt; SelectListItem & gt; Country = new list & lt; SelectListItem & gt; New SelectListItem {Value = "0", Text = "Select Country ..."}, New SelectListItem {Value = "1", Text = "US United States"}, New SelectListItem {Value = "2", Text = "AF Afghanistan"}, ...}}   

And in my opinion. Cshtml:

  & lt; Div class = "col-lg-6 extension-correct" & gt; @HTML.DRAFDOWN LISTFORE (M = & gt; MLLLLLL Entity. Address: Country.IED, New Selection List (Model Legal Answer Address Country Country, "Value", "Text", Model Legal Answer Addresses. Country ID, new {@class = "form-control"}) & lt; / Div & gt;   

The drop down is being populated with the desired items, and the correct address ID is being posted on the server. I think the problem is with Automapers in the code of controller's code viewing model for each country to create a new line in the country table. What is supposed to do, is adding a value to the foreign key in the address table, indicating the appropriate address:

  Create a public function (SiteViewModel siteViewModel) {if (ModelState.IsValid) {Site NewSite = New Site (); NewSite = Mapper Map & lt; SiteViewModel, Site & gt; (SiteVodel); NewSite.DateCreated = DateTime.Now; _unitOfWork.SiteRepository.Insert (newsite); _unitOfWork.Save (); ...}}   

How do I get an automaker to do this?

Mark

=== More info ===

There is an address ViewModel in my SiteViewModel, like this:

  Public Square SiteViewModel {... Public Virtual Address ViewModel Address {get; Set; } ...   

and my addressmodel looks like this:

  public class address ViewModel {... [display (name = "country" )] Public virtual countrymodal country {receive; Set; } ...}}    

OK, im going to answer it Without all information

Actually you are mapping the CountryViewModel country to the 'Domain object', your ORM is expecting.

Automapping is essentially running

  new country {property = @value};   

Creating a new country object that is not attached to your ORM Your ORM is creating these objects in the database instead.

What you want to do instead, is using the information in ViewModel to query the database for the country object in the database and using it within your site. Similar to the following:

  create public action roles (site visual mathora sitemodel) {if (ModelState.IsValid) {site new site = new site (); NewSite = Mapper Map & lt; SiteViewModel, Site & gt; (SiteVodel); New site.address.ountry = dbcontext.countries.firstOrDefault (x = & gt; x.Id == See siteModel.Edress.Design.DEIID); NewSite.DateCreated = DateTime.Now; _unitOfWork.SiteRepository.Insert (newsite); _unitOfWork.Save (); ...}}   

It really raises the fact that what you were trying to support the original design of the automaker was not the launch of your data layer The layers of the view used to help in the object, not the other way.

Comments

Popular posts from this blog

ios - Adding an SKSpriteNode to SKScene from a child SKSpriteNode -

Matlab transpose a table vector -

c# - Textbox not clickable but editable -