Click here to Skip to main content
14,775,017 members
Home » Discussions » ASP.NET
   

ASP.NET

 
SuggestionRe: VS2012 cannot open MVC4 application - the system cannot find the file specified (exception from HRESULT 0x80070002) Pin
Kornfeld Eliyahu Peter16-Aug-16 21:06
professionalKornfeld Eliyahu Peter16-Aug-16 21:06 
GeneralRe: VS2012 cannot open MVC4 application - the system cannot find the file specified (exception from HRESULT 0x80070002) Pin
Usman ali24-Aug-16 23:27
MemberUsman ali24-Aug-16 23:27 
QuestionAsp.net C# Export to excel stop working Pin
BigLitz15-Aug-16 7:57
MemberBigLitz15-Aug-16 7:57 
AnswerRe: Asp.net C# Export to excel stop working Pin
Richard Deeming15-Aug-16 8:41
mveRichard Deeming15-Aug-16 8:41 
GeneralRe: Asp.net C# Export to excel stop working Pin
BigLitz15-Aug-16 14:14
MemberBigLitz15-Aug-16 14:14 
Questionhow to make delete row from table using linq Pin
ahmed_sa14-Aug-16 8:20
Memberahmed_sa14-Aug-16 8:20 
AnswerRe: how to make delete row from table using linq Pin
John C Rayan16-Aug-16 2:30
professionalJohn C Rayan16-Aug-16 2:30 
QuestionTrying to EDIT 2 separate, one-to-one database tables with ONE form/ViewModel Pin
TheOnlyRealTodd11-Aug-16 21:32
professionalTheOnlyRealTodd11-Aug-16 21:32 
Is there any way to somehow combine the data from two models and THEN map them *both* to the same viewModel in the context of an edit action?

I have never had to update several tables at once in an edit action in ASP.NET MVC with Entity Framework 6.1.3. This is the layout:

I have a DB table called "Address" which has fields for StreetNumber, StreetName, City, State, ZipCode. It has a *one-to-one* relationship with another table called Bars. As in, a bar can only have one address and one address can only have one bar.

Because I am storing this data in two separate tables, I am having a very difficult time trying to successfully implement an Edit action which takes data from **one form** (BarForm) and should update both the Bar and Address database tables. See my code:

**BarController**
public ActionResult Edit(int id)
        {

            var bar = _context.Bars.SingleOrDefault(m => m.Id == id);
            var address = _context.Addresses.SingleOrDefault(a => a.BarId == id);
            //Make sure that the id actually exists:
            if (bar == null)
            {
                return HttpNotFound();
            }
            var viewModel = Mapper.Map<Bar, BarFormViewModel>(bar, new BarFormViewModel());
            if (address == null)
            {
                address = new Address();
            }
            Mapper.Map<Address, BarFormViewModel>(address, viewModel);

            viewModel.IsNew = false;

            return View("BarForm", viewModel);
        }
        [ValidateAntiForgeryToken]
        public ActionResult Save(BarFormViewModel bar)
        {

            if (!ModelState.IsValid)
            {
                var viewModel = Mapper.Map<BarFormViewModel, BarFormViewModel>(bar, new BarFormViewModel());
                viewModel.IsNew = false;
                return View("BarForm", viewModel);

            }
            if (bar.Id == 0)
            {

                var newbar = Mapper.Map<BarFormViewModel, Bar>(bar);
                newbar.LastUpdated = DateTime.UtcNow;
                _context.Bars.Add(newbar);
                var addressToAdd = Mapper.Map<BarFormViewModel, Address>(bar);
                _context.Addresses.Add(addressToAdd);

            }
            else
            {
                var barInDb = _context.Bars.Single(b => b.Id == bar.Id);
                var addressInDb = _context.Addresses.Single(a => a.BarId == bar.Id);
                Mapper.Map<BarFormViewModel, Bar>(bar, barInDb);
                Mapper.Map<BarFormViewModel, Address>(bar, addressInDb);

            }
            _context.SaveChanges();

            return RedirectToAction("Index", "Bar");
        }


**Domain Models:**

public class Bar
        {
            public int Id { get; set; }
            public string Name { get; set; }
            [Required]
            public string GooglePlaceId { get; set; }
            public string SundayDiscounts { get; set; }
            public string MondayDiscounts { get; set; }
            public string TuesdayDiscounts { get; set; }
            public string WednesdayDiscounts { get; set; }
            public string ThursdayDiscounts { get; set; }
            public string FridayDiscounts { get; set; }
            public string SaturdayDiscounts { get; set; }
            [Display(Name = "Last Updated")]
            public DateTime LastUpdated { get; set; }
        }
    
     public class Address
        {
            public int Id { get; set; }
            public int? Number { get; set; }
            public string StreetName { get; set; }
    
            public string City { get; set; }
    
            public string State { get; set; }
            [Required]
            public int ZipCode { get; set; }
    
            public Bar Bar { get; set; }
            public int BarId { get; set; }
        }

**View Model which includes both Address and Bar properties**:


{
        public class BarFormViewModel
        {
            public int? Id { get; set; }
            public string Name { get; set; }
            [Required]
            [Display(Name = "Google Place ID")]
            public string GooglePlaceId { get; set; }
            [Display(Name = "Sunday Happy Hour Info:")]
            public string SundayDiscounts { get; set; }
            [Display(Name = "Monday Happy Hour Info:")]
            public string MondayDiscounts { get; set; }
            [Display(Name = "Tuesday Happy Hour Info:")]
            public string TuesdayDiscounts { get; set; }
            [Display(Name = "Wednesday Happy Hour Info:")]
            public string WednesdayDiscounts { get; set; }
            [Display(Name = "Thursday Happy Hour Info:")]
            public string ThursdayDiscounts { get; set; }
            [Display(Name = "Friday Happy Hour Info:")]
            public string FridayDiscounts { get; set; }
            [Display(Name = "Saturday Happy Hour Info:")]
            public string SaturdayDiscounts { get; set; }
            [Display(Name = "Last Updated")]
            public DateTime? LastUpdated { get; set; }
    
    
            //Address Model Info
            public Address Address { get; set; }
            public int? AddressId { get; set; }
            [RegularExpression("([1-9][0-9]*)", ErrorMessage = "Must be a number")]
            public int? Number { get; set; }
            public string StreetName { get; set; }
    
            public string City { get; set; }
    
            public string State { get; set; }
            [Required]
            public int? ZipCode { get; set; }
    
            public bool IsNew { get; set; }
    
        }


The problem here is that I am getting an empty AddressId with this setup, which is causing an exception when the Save action gets run. This is because the BarForm view is getting passed a ViewModel which has been mapped from a Bar object and the Bar domain model actually has no Address information in it, since it is not the Address model/table.

Is there any way to somehow combine the data from both the Address and Bar models and THEN map them *both* to the same viewModel?

I keep getting a Sequence Contains no Elements error for this line in the Save action:

var addressInDb = _context.Addresses.Single(a => a.Id == bar.AddressId);

I also tried:

var addressInDb = _context.Addresses.Single(a => a.BarId == bar.Id);


Neither work. I understand what the error is saying and have also checked the actual HTML for my hidden Addressid field and it is blank... See code in my BarForm View:

@Html.HiddenFor(m => m.Id)
       @Html.HiddenFor(m => m.AddressId)
       @Html.AntiForgeryToken()

AnswerRe: Trying to EDIT 2 separate, one-to-one database tables with ONE form/ViewModel Pin
F-ES Sitecore12-Aug-16 1:34
mveF-ES Sitecore12-Aug-16 1:34 
QuestionPrintPageEventHandler not working when I upload my site to IIS Pin
awinash kr6-Aug-16 20:21
Memberawinash kr6-Aug-16 20:21 
QuestionRe: PrintPageEventHandler not working when I upload my site to IIS Pin
Richard MacCutchan6-Aug-16 21:24
mveRichard MacCutchan6-Aug-16 21:24 
AnswerRe: PrintPageEventHandler not working when I upload my site to IIS Pin
awinash kr6-Aug-16 21:28
Memberawinash kr6-Aug-16 21:28 
SuggestionRe: PrintPageEventHandler not working when I upload my site to IIS Pin
Kornfeld Eliyahu Peter6-Aug-16 23:35
professionalKornfeld Eliyahu Peter6-Aug-16 23:35 
GeneralRe: PrintPageEventHandler not working when I upload my site to IIS Pin
awinash kr6-Aug-16 23:47
Memberawinash kr6-Aug-16 23:47 
AnswerRe: PrintPageEventHandler not working when I upload my site to IIS Pin
Richard Deeming8-Aug-16 2:35
mveRichard Deeming8-Aug-16 2:35 
Questionwebform Pin
Member 126713194-Aug-16 22:46
MemberMember 126713194-Aug-16 22:46 
AnswerRe: webform Pin
Richard MacCutchan4-Aug-16 23:09
mveRichard MacCutchan4-Aug-16 23:09 
Questioninstallation of asp.net Pin
AMIT SINGH 20164-Aug-16 9:27
MemberAMIT SINGH 20164-Aug-16 9:27 
QuestionCould not find a part of the path "\ASP.Net Practical\Website\Site1\UploadedImgs \".. Pin
Otekpo Emmanuel2-Aug-16 7:33
MemberOtekpo Emmanuel2-Aug-16 7:33 
AnswerRe: Could not find a part of the path "\ASP.Net Practical\Website\Site1\UploadedImgs \".. Pin
jkirkerx2-Aug-16 10:19
professionaljkirkerx2-Aug-16 10:19 
GeneralRe: Could not find a part of the path "\ASP.Net Practical\Website\Site1\UploadedImgs \".. Pin
Otekpo Emmanuel3-Aug-16 8:16
MemberOtekpo Emmanuel3-Aug-16 8:16 
GeneralRe: Could not find a part of the path "\ASP.Net Practical\Website\Site1\UploadedImgs \".. Pin
jkirkerx3-Aug-16 8:40
professionaljkirkerx3-Aug-16 8:40 
QuestionRegarding DataTable width Pin
harikreddy31-Jul-16 22:48
Memberharikreddy31-Jul-16 22:48 
QuestionRe: Regarding DataTable width Pin
Richard MacCutchan31-Jul-16 23:24
mveRichard MacCutchan31-Jul-16 23:24 
AnswerRe: Regarding DataTable width Pin
ZurdoDev1-Aug-16 9:11
professionalZurdoDev1-Aug-16 9:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.