Editing/Listing multiple models within one view - c#

I am new to the MVC and EF world. I am targeting MVC 4 EF 5 using code first.
I am looking for the best practice for editing two related models using one view. For simplicity I have the following two Models:
namespace AddressBook.Models
{
public class Contact
{
public int ID { get; set; }
public string First_Name { get; set; }
public string Last_Name { get; set; }
public List<PhoneNumber> PhoneNumbers { get; set; }
}
}
and
namespace AddressBook.Models
{
public class PhoneNumber
{
public int ID { get; set; }
public string Number { get; set; }
public bool Primary { get; set; }
}
}
with the following context:
using System.Data.Entity;
namespace AddressBook.Models
{
public class DataContext : DbContext
{
public DbSet<Contact> Contacts { get; set; }
public DbSet<PhoneNumber> PhoneNumbers { get; set; }
}
}
The relationship between the Contact and PhoneNumber is one to many, However I would like to be able to edit the first_name, last_name and Number when the Primary is set to true, so we would be editing only one phone number per contact record.
I have seen similar posts that point to using a ViewModel but the only examples of viewmodels I have seen are when used instead of the viewbag when passing the information for a dropdown.
I guess I have a few questions:
would the ViewModel look like below?
public class ContactPrimaryNumberViewModel
{
public Contact ContactToEdit {get; set;}
public PhoneNumber PhoneNumberToEdit {get;set;}
}
what would the edit(post) and edit(get) look like?
Any help would be appreciated to help me wrap my head around this ...
here is the Edit(get) modified to support if contact does not have phone number associated
' // GET: /Contact/Edit/5
public ActionResult Edit(int id = 0)
{
ContactPrimaryNumberViewModel ContactPrimaryNumber = (from pn in db.PhoneNumbers
where pn.ContactID == id && pn.Primary == true
select new ContactPrimaryNumberViewModel { ContactID = pn.ContactID, First_Name = pn.Contact.First_Name, Last_Name = pn.Contact.Last_Name, Number = pn.Number }).SingleOrDefault();
if (ContactPrimaryNumber == null)
{
ContactPrimaryNumber = (from c in db.Contacts
where c.ID == id
select new ContactPrimaryNumberViewModel { ContactID = c.ID, First_Name = c.First_Name, Last_Name = c.Last_Name, Number = null }).Single();
}
return View(ContactPrimaryNumber);
}'
so the final solution after everyones help is:
the models:
public class PhoneNumber
{
public int ID { get; set; }
public string Number { get; set; }
public bool Primary { get; set; }
[Required]
public int ContactID { get; set; }
public Contact Contact { get; set; }
}
public class Contact
{
public int ID { get; set; }
public string First_Name { get; set; }
public string Last_Name { get; set; }
public List<PhoneNumber> PhoneNumbers { get; set; }
}
The controler edit(get and post)
// GET: /Contact/Edit/5
public ActionResult Edit(int id = 0)
{
ContactPrimaryNumberViewModel ContactPrimaryNumber = (from c in db.Contacts
join pn in db.PhoneNumbers
on c.ID equals pn.ContactID into outer
from _pn in outer.Where(p => p.Primary ==true).DefaultIfEmpty()
where c.ID == id
select new ContactPrimaryNumberViewModel { ContactID = c.ID, First_Name = c.First_Name, Last_Name = c.Last_Name, Number = ((_pn == null) ? "" : _pn.Number) }).FirstOrDefault();
if (ContactPrimaryNumber == null)
{
return HttpNotFound();
}
return View(ContactPrimaryNumber);
}
// POST: /Contact/Edit/5
[HttpPost]
public ActionResult Edit(ContactPrimaryNumberViewModel ContactPrimaryNumber)
{
Contact c = db.Contacts.Find(ContactPrimaryNumber.ContactID);
PhoneNumber pn = db.PhoneNumbers.FirstOrDefault(x => x.ContactID == ContactPrimaryNumber.ContactID && x.Primary == true);
if (ModelState.IsValid)
{
c.First_Name = ContactPrimaryNumber.First_Name;
c.Last_Name = ContactPrimaryNumber.Last_Name;
if (pn == null) //if there is no phone number associated with the contact in the DB
{
if (!String.IsNullOrEmpty(ContactPrimaryNumber.Number))
{
//Add a new phonenumber in the database
PhoneNumber Px = new PhoneNumber();
Px.ContactID = ContactPrimaryNumber.ContactID;
Px.Number = ContactPrimaryNumber.Number;
Px.Primary = true;
db.PhoneNumbers.Add(Px);
}
}
else //if there is a phone number associated with the contactin the DB
{
if (String.IsNullOrEmpty(ContactPrimaryNumber.Number))
{
//delete the existing number
db.PhoneNumbers.Remove(pn);
}
else
{
//modify the existing number
pn.Number = ContactPrimaryNumber.Number;
}
}
db.SaveChanges();
return RedirectToAction("Index");
}
return View(c);
}
and the viewmodel
public class ContactPrimaryNumberViewModel
{
public int ContactID { get; set; }
public string First_Name { get; set; }
public string Last_Name { get; set; }
public string Number { get; set; }
}
thanks again for your help

I think your view model should look like this:
public class ContactPrimaryNumberViewModel
{
public int ID { get; set; }
public string First_Name { get; set; }
public string Last_Name { get; set; }
public string Number { get; set; }
}
And your update would look something like:
Contact c = context.Contacts.Find(id);
PhoneNumber p = context.PhoneNumbers
.FirstOrDefault(x => x.id == id && x.Primary == true);
//validate input
//update as necessary
//SaveChanges() etc...
from your comment - you new up the model class ContactPrimaryNumberViewModel:
var ContactPrimaryNumber =
from pn in db.PhoneNumbers
where pn.ContactID == id && pn.Primary == true
select new ContactPrimaryNumberViewModel() {
ContactID = pn.ContactID,
First_Name = pn.Contact.First_Name,
Last_Name = pn.Contact.Last_Name,
Number = pn.Number
};

Okay, try this:
PhoneNumber
public class PhoneNumber
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string Number { get; set; }
public bool Primary { get; set; }
[ForeignKey("Contact"), DatabaseGenerated(DatabaseGeneratedOption.None)]
public int? ContactId { get; set; }
public virtual Contact Contact { get; set; }
}
Contact
public class Contact
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ContactId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public List<PhoneNumber> PhoneNumbers { get; set; }
}
SaveContact: depending on how you have set up your Repositories or Ef class. This can either go in your EfRepository implemention or your EfDb class.
public void SavePlayer(Contact contact)
{
using (var context = new EfDb())
{
if (contact.ContactId == 0)
{
context.Contacts.Add(contact);
}
else if (contact.ContactId > 0)
{
var currentContact = context.Contacts
.Include(c => c.PhoneNumber)
.Single(c => c.ContactId== contact.ContactId);
context.Entry(currentContact).CurrentValues.SetValues(contact);
currentContact.PhoneNumber= contact.PhoneNumber;
}
context.SaveChanges();
}
}
Edit action
[HttpGet]
public ActionResult Edit(int id)
{
var contact= _dataSource.Contacts.FirstOrDefault(c => c.Id == id);
return View(player);
}
[HttpPost]
public ActionResult Edit(Contact contact)
{
try
{
if (ModelState.IsValid)
{
_dataSource.SaveContact(contact);
return RedirectToAction("About", "Home");
}
}
catch (Exception)
{
ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists, see your system administrator.");
}
return View(contact);
}
Views
In your Contact View Folder add EditorTemplates folder. Then Scaffold a Create Strongly Typed PhoneNumber Partial View to this folder and name it PhoneNumber like its model.
Scaffold a Create Strongly Typed Contact View name it Create
Then add #Html.EditorFor(model => model.PhoneNumber) to the master Create View.

Related

how i update multiple table using viewmodel in Asp.net mvc

Employee Model
public class Employee
{
[Key]
public int EmployeeID { get; set; }
public string Name { get; set; }
public virtual Department Departments { get; set; }
public int DepartmentID { get; set; }
}
Department Model
public class Department
{
public int DepartmentID { get; set; }
public string DepartmentName { get; set; }
}
View Model for Department and Employee
public class EDViewModel
{
public int ID { get; set; }
public int EmployeeID { get; set; }
public string Name { get; set; }
public Department Departments { get; set; }
public int DepartmentID { get; set; }
public string DepartmentName { get; set; }
}
Now i want to update both tables with single view.
Controller
public ActionResult Edit(int?id)
{
// write some code for update both table at once time
}
PostMethod
[HttpPost]
public ActionResult Edit(EDViewModel Emodel)
{
var user = db.Employees.Where(c => c.Employee_Id == Emodel.Employee_Id).FirstOrDefault();
user.UserName = Emodel.UserName;
user.ProfilePicture = Emodel.ProfilePicture;
db.Entry(user).State = EntityState.Modified;
db.Entry(user).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Home");
}
But in this method only Update Employee record not department
After many searching finally i get a solid solution
hope you like or improve this.
Controller
public ActionResult Edit(int? id)
{
MYDb db = new MYDb();
var user = db.Employees.Where(c => c.Employee_Id == Emodel.Employee_Id).FirstOrDefault();
if (user != null)
{
var vm = new EDViewModel { Employee_id = user.Employee_id, departmentName = user.departmentName };
if (user.department != null)
{
user.Departmet_id = vm.Departments.Departmet_id;
user.DepartmentName = vm.Departments.DepartmentName;
user.Employee_id = vm.employee_id;
user.employeeName = vm.employeeName;
}
return View(user);
}
return Content("Invalid Id");
}
[HttpPost]
public ActionResult Edit(EDViewModel Emodel)
{
var user = db.Employees.Where(c => c.Employee_Id == Emodel.Employee_Id).FirstOrDefault();
user.EmployeeId = Emodel.EmployeeId;
user.EmployeeName= Emodel.EmployeeName;
user.DepartmentName= Emodel.Departmt.DepartmentName;
// Just remove this line
// db.Entry(user).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Home");
}
it worked for me
just remove this
db.Entry(user).State = EntityState.Modified;
if we not remove this entityvalidation occur

Issue to compose a nested relation query with Entity Framework

my relation is hierarchical like customer -> address -> contacts
a single customer may have multiple address and a single address may have multiple contacts. see my class structures.
public class CustomerBase
{
public int CustomerID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
public class Customer : CustomerBase
{
public virtual List<Addresses> Addresses { get; set; }
}
public class Addresses
{
[Key]
public int AddressID { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public bool IsDefault { get; set; }
public virtual List<Contacts> Contacts { get; set; }
public int CustomerID { get; set; }
public virtual Customer Customer { get; set; }
}
public class Contacts
{
[Key]
public int ContactID { get; set; }
public string Phone { get; set; }
public string Fax { get; set; }
public int AddressID { get; set; }
public virtual Addresses Customer { get; set; }
}
public class TestDBContext : DbContext
{
public TestDBContext()
: base("name=TestDBContext")
{
}
public DbSet<Customer> Customer { get; set; }
public DbSet<Addresses> Addresses { get; set; }
public DbSet<Contacts> Contacts { get; set; }
}
this way i am population data in db table with EF code first.
using (var db = new TestDBContext())
{
var customer = new Customer
{
FirstName = "Test Customer2",
LastName = "Test Customer2",
Addresses = new List<Addresses>
{
new Addresses
{
Address1 = "foo1",
Address2 = "foo2",
IsDefault=true,
Contacts = new List<Contacts>
{
new Contacts { Phone = "22222222", Fax = "1-999999999" }
}
}
}
};
db.Customer.Add(customer);
db.SaveChanges();
now i want to query data. suppose i want to fetch customer whose customerid is 1 and want to load address related to customer id is 1 and address Isdefault is true and default address related contacts details.
i try to compose it this way hence could not complete because i am in EF and LINQ query.
var bsCustomer = db.Customer.Where(c => c.CustomerID == 2).Include(a=>
a.Addresses.Where(a=> a.IsDefault==true)).Include(c=> c.)
so please tell me what will be the query as a result customer id 1 related address will load whose isdefault would be true and address related contact will be loaded. thanks
You can try as shown below.
Query based syntax :
var dbquery = from cu in db.Customers
where (cu.CustomerID == 1)
select new {
cu,
Addresses= from ad in cu.Addresses
where (ad.IsDefault == true)
from ct in ad.Contacts
select ad,
};
You can iterate it as you wish :
var customers = dbquery.AsEnumerable()
.Select(c => c.cu);
foreach(var customer in customers )
{
foreach(var address in customer.Addresses)
{
//your code;
}
}
Method based syntax :
var dbquery = db.Customers.Where(cu=>cu.CustomerID == 1)
.Select(cus=> new {
cus,
Addresses= cus.Addresses.Where(ad=>ad.IsDefault == true).Include(c=>c.Contacts)
}).AsEnumerable()
.Select(f => f.cus).ToList();

Can't create many-to-many relationship

I have two entites with a many-to-many relationship. Company and SearchKeyword.
Here are the models:
class SearchKeyword
{
public int ID { get; set; }
public string Text { get; set; }
public virtual ICollection<Company> Companies { get; set; }
}
class Company
{
public int ID { get; set; }
public string Name { get; set; }
public virtual OtherDetail OtherDetails { get; set; }
public virtual ICollection<SearchKeyword> SearchKeywords { get; set; }
}
I am trying to add a SearchKeyword to a company but it won't let me. I tried this:
using (var db = new PlaceDBContext())
{
Company c = db.Companies.Single(x => x.ID == 1);
SearchKeyword sk = db.SearchKeywords.Single(x => x.ID == 1);
c.SearchKeywords.Add(sk);
db.SaveChanges();
}
It says Object reference not set to an instance of an object. I am not sure what is null. In inspector I can see c and sk both have full values. I guess I must be missing a fundamental of how the many-to-many relationship works with EF.
What am I doing wrong?
This is because SearchKeywords is null.
Either you can assign a List to it before adding a new instance
using (var db = new PlaceDBContext())
{
Company c = db.Companies.Single(x => x.ID == 1);
SearchKeyword sk = db.SearchKeywords.Single(x => x.ID == 1);
c.SearchKeywords = new List<SearchKeyword>();
c.SearchKeywords.Add(sk);
db.SaveChanges();
}
Or you can do it constructor method
class Company
{
public Company()
{
SearchKeywords = new List<SearchKeyword>();
}
public int ID { get; set; }
public string Name { get; set; }
public virtual OtherDetail OtherDetails { get; set; }
public virtual ICollection<SearchKeyword> SearchKeywords { get; set; }
}

how to manipulate a list of properties in a class when sending one email?

I'm trying to implement order confirmation in application (MVC 4) which uses a shopping cart.
I'm at the stage where I want to send out an email to both the customer and Admin user to confirm the order.
I want to send one email for one order(Cart).
My Cart class.
I have a Cart Model:
namespace MerchandiseProject.Domain.Entities
{
public class Cart
{
private List<CartLine> lineCollection = new List<CartLine>();
public void AddItem(Product product, int quantity)
{
CartLine line = lineCollection
.Where(p => p.Product.ProductID == product.ProductID)
.FirstOrDefault();
if (line == null)
{
lineCollection.Add(new CartLine { Product = product,
Quantity = quantity });
}
else
{
line.Quantity += quantity;
}
}
public void RemoveLine(Product product)
{
lineCollection.RemoveAll(l => l.Product.ProductID == product.ProductID);
}
public decimal ComputeTotalValue()
{
return lineCollection.Sum(e => e.Product.Price * e.Quantity);
}
public void Clear()
{
lineCollection.Clear();
}
public IEnumerable<CartLine> Lines
{
get { return lineCollection; }
}
}
public class CartLine
{
public Product Product { get; set; }
public int Quantity { get; set; }
}
}
I am passing this model and the shipping details model back to POST checkout actionResult in my Cartcontroller and assigning their values to an Orders class, these details are also persisted to the database.
The information being past back to the database are correct.
But how to extract it in the likes of a "foreach" statement has me stuck !
Orders Class:
namespace MerchandiseProject.Domain.Entities
{
public class Orders
{
private List<OrderLine> lineCollection = new List<OrderLine>();
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int OrderId { get; set; }
public DateTime OrderDate { get; set; }
public int ProductID { get; set; }
public string ProductName { get; set; }
public decimal ProductUnitPrice { get; set; }
public int Quantity { get; set; }
public decimal OrderTotal { get; set; }
public string Name { get; set; }
public string UserName { get; set; }
public string Line1 { get; set; }
public string Line2 { get; set; }
public string Line3 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zip { get; set; }
public string Country { get; set; }
public bool GiftWrap { get; set; }
public IEnumerable<OrderLine> Lines
{
get { return lineCollection; }
}
}
public class OrderLine
{
public Product Product { get; set; }
public int Quantity { get; set; }
}
}
My problem is in my Cart Controller Checkout ViewResult [POST], an email goes out for every type of product in the cart, I want to send one e-mail with the list of products ordered ? make sense ?
Cart Controller:
[HttpPost]
public ViewResult Checkout(Cart cart, ShippingDetails shippingDetails)
{
var CurrentUser = HttpContext.User;
string currentUserName = CurrentUser.Identity.Name;
if (cart.Lines.Count() == 0)
{
ModelState.AddModelError("", "Sorry, your cart is empty!");
}
if (ModelState.IsValid)
{
foreach (var line in cart.Lines)
{
var model = new Orders()
{
ProductName = line.Product.Name,
ProductUnitPrice = line.Product.Price,
Quantity = line.Quantity,
OrderTotal = line.Quantity * line.Product.Price,
UserName = currentUserName,
Name = shippingDetails.Name,
Line1 = shippingDetails.Line1,
Line2 = shippingDetails.Line2,
Line3 = shippingDetails.Line3,
City = shippingDetails.City,
State = shippingDetails.State,
Country = shippingDetails.Country,
GiftWrap = shippingDetails.GiftWrap,
OrderDate = DateTime.Now,
Zip = shippingDetails.Zip,
};
new MailController(_actionMailer).OrderDetailsEmail(model).Deliver();
}
orderProcessor.SaveOrder(currentUserName ,cart, shippingDetails);
cart.Clear();
return View("Completed");
}
else
{
return View(shippingDetails);
}
}
Please note - asp.net MVC 4 using razor views.
As #DStanley said, you are literally saying to send an email for each line item by having your MailController line within the foreach loop. If the problem is you need access to model outside the context of the foreach loop, then you need to persist it in a variable in a parent scope. For example:
var lineItems = new List<Order>();
foreach (var line in cart.Lines)
{
lineItems.Add(new Order
{
...
});
}
However, if you're going to do that, it's more efficient to just use Select:
var lineItems = cart.Lines.Select(m => new Order
{
ProductName = m.Product.Name,
...
});
Removing the need for the foreach completely.

C# EF add data to model

I have those models
class Artist
{
public int Id { get; set; }
[StringLength(500)]
public string Name { get; set; }
[StringLength(50)]
public string LastName { get; set; }
public virtual ICollection<SimilarArtist> SimilarArtists { get; set; }
}
class SimilarArtist
{
public int Id { get; set; }
public int ArtistId { get; set; }
[ForeignKey("ArtistId")]
public Artist Artist { get; set; }
public int Similar_Artist_Id { get; set; }
}
So each artist have links to other 5 from the same table. When the migration generate database it made that stracture.
SELECT [Id]
,[Name]
,[LastName]
FROM [dbo].[Artists]
SELECT [Id]
,[ArtistId]
,[Similar_Artist_Id]
FROM [dbo].[SimilarArtists]
So when I do select the model it return this
var similar = _db.Artists.FirstOrDefault(x => x.Name == id).SimilarArtists.FirstOrDefault();
//similar.ArtistId
//similar.Id
//similar.Similar_Artist_Id
//similar.Artist //the object which return main artist
The question is how I can get in "var similar" not just Similar_Artist_Id but also name and lastname in the same request (without making requests by Similar_Artist_Id)
var similarId = model.SimilarArtists.FirstOrDefault().Id;
var artiest = _db.Artists.Where(x.Id = similarId);
or just:
similar.Artist.Name
Or if you want to be able to have strongly-type property such as similar.ArtistName, create a [NotMapped] getter property.
class SimilarArtist
{
public int Id { get; set; }
public int ArtistId { get; set; }
[ForeignKey("ArtistId")]
public Artist Artist { get; set; }
public int Similar_Artist_Id { get; set; }
[NotMapped]
public string ArtistName
{
get
{
return this.Artist.Name;
}
}
}
You can do
var similar = _db.Artists.Where(x => x.Name == id)
.Select(a => a.SimilarArtists.FirstOrDefault())
.FirstOrDefault();
This gives you the first SimilarArtists (with all of its properties) of the first Artists matching the predicate x.Name == id.

Categories

Resources